如何仅使用一个 for 循环反转多个列表框中的所选项目?

发布于 2024-10-15 07:04:19 字数 80 浏览 1 评论 0原文

我正在使用三个列表框。我必须使用反转按钮反转所有列表框中的选定项目。

如何仅使用单个循环对其进行编码?也可以有超过 3 个列表框。

I am using three list boxes. I have to invert the selected items in all the list boxes using an invert button.

How can code it using only a single loop? There can be more than 3 list boxes as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蝶舞 2024-10-22 07:04:19

您好,您可以使用此函数来反转给定列表框的选择。

    /* Windows ListBox
    public void InvertSelection(ListBox objLstbox)
    {
        if(objLstbox == null) return;

        for (int i = 0; i < objLstbox.Items.Count; i++)
            objLstbox.SetSelected(i, !objLstbox.GetSelected(i));
    }
    */

    //WebApp listbox
    public void InvertSelection(ListBox objLstbox)
    {
        if (objLstbox == null) return;

        for (int i = 0; i < objLstbox.Items.Count; i++)
            objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected; 
    }

    private void btnInvert_Click(object sender, EventArgs e)
    {
        InvertSelection(listBox1);
        InvertSelection(listBox2);
        InvertSelection(listBox3);
    }

Hi you could use this function to invert the selection for a given listbox.

    /* Windows ListBox
    public void InvertSelection(ListBox objLstbox)
    {
        if(objLstbox == null) return;

        for (int i = 0; i < objLstbox.Items.Count; i++)
            objLstbox.SetSelected(i, !objLstbox.GetSelected(i));
    }
    */

    //WebApp listbox
    public void InvertSelection(ListBox objLstbox)
    {
        if (objLstbox == null) return;

        for (int i = 0; i < objLstbox.Items.Count; i++)
            objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected; 
    }

    private void btnInvert_Click(object sender, EventArgs e)
    {
        InvertSelection(listBox1);
        InvertSelection(listBox2);
        InvertSelection(listBox3);
    }
森林迷了鹿 2024-10-22 07:04:19
public void InvertSelection(ListBox objLstbox)
{
    if (objLstbox == null) return;

    for (int i = 0; i < objLstbox.Items.Count; i++)
        objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected; 
}

protected void Button1_Click(object sender, EventArgs e)
{
    InvertSelection(ListBox1);
}
public void InvertSelection(ListBox objLstbox)
{
    if (objLstbox == null) return;

    for (int i = 0; i < objLstbox.Items.Count; i++)
        objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected; 
}

protected void Button1_Click(object sender, EventArgs e)
{
    InvertSelection(ListBox1);
}
翻了热茶 2024-10-22 07:04:19

我和你们其他人一起研究这个问题,最后开发了我自己的反转功能
这是 VB.Net 的答案:

Private Function InvertListBoxSelections(ByRef tempListBox As ListBox) As Integer
    Dim selectedind(tempListBox.SelectedItems.Count) As Integer
    Try
        For selind = 0 To tempListBox.SelectedItems.Count - 1
            selectedind.SetValue(tempListBox.Items.IndexOf(tempListBox.SelectedItems(selind)), selind)
        Next
        tempListBox.ClearSelected()
        For listitemIndex = 0 To tempListBox.Items.Count
            If Array.IndexOf(selectedind, listitemIndex) < 0 Then
                tempListBox.SetSelected(listitemIndex, True)
            End If
        Next
        Return 1
    Catch ex As Exception
        Return 0
    End Try
End Function

I banged my head on this with the rest of you, and finally developed my own function for Inverting
Here's the VB.Net Answer:

Private Function InvertListBoxSelections(ByRef tempListBox As ListBox) As Integer
    Dim selectedind(tempListBox.SelectedItems.Count) As Integer
    Try
        For selind = 0 To tempListBox.SelectedItems.Count - 1
            selectedind.SetValue(tempListBox.Items.IndexOf(tempListBox.SelectedItems(selind)), selind)
        Next
        tempListBox.ClearSelected()
        For listitemIndex = 0 To tempListBox.Items.Count
            If Array.IndexOf(selectedind, listitemIndex) < 0 Then
                tempListBox.SetSelected(listitemIndex, True)
            End If
        Next
        Return 1
    Catch ex As Exception
        Return 0
    End Try
End Function
满地尘埃落定 2024-10-22 07:04:19
    for (int i = 0; i < listbox.Items.Count; i++)
    {
        if (listbox.SelectedItems.Contains(listbox.Items[i]))
            listbox.SetSelected(i, false);
        else
            listbox.SetSelected(i, true);
    }
    for (int i = 0; i < listbox.Items.Count; i++)
    {
        if (listbox.SelectedItems.Contains(listbox.Items[i]))
            listbox.SetSelected(i, false);
        else
            listbox.SetSelected(i, true);
    }
夜清冷一曲。 2024-10-22 07:04:19

由于我来到这里并对选择感到困惑,所以我将把它留在这里。

此代码使用 .SetItemChecked.GetItemChecked 反转所有选中的项目:

private void ButtonInvertChecked_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < checkedListBox.Items.Count; i++)
        checkedListBox.SetItemChecked (i, !checkedListBox.GetItemChecked(i));
    }

Since I got here and got confused by selection i'll leave this here.

This code inverts all checked items by using .SetItemChecked and .GetItemChecked:

private void ButtonInvertChecked_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < checkedListBox.Items.Count; i++)
        checkedListBox.SetItemChecked (i, !checkedListBox.GetItemChecked(i));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文