Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C列表框在多重扩展模式下选择和取消选择项_C#_Listbox - Fatal编程技术网

C# C列表框在多重扩展模式下选择和取消选择项

C# C列表框在多重扩展模式下选择和取消选择项,c#,listbox,C#,Listbox,当我单击列表框中未选中的项目时,它将被选中。客户端希望,如果您再次单击而不使用cntrl键,它将取消选择 但我尝试了很多东西,但都不管用。那么这可能吗?如果可能的话,有人能解释一下如何使用一些C代码吗?您可以在selected index事件中添加一些内容,如果selected index与Precious selected相同,则将其存储在某个位置,然后将selected index设置为-1,因此不选择任何选项。使用内置选项无法轻松做到这一点。我的解决方案是,当鼠标位于控件上方时,通过编程发

当我单击列表框中未选中的项目时,它将被选中。客户端希望,如果您再次单击而不使用cntrl键,它将取消选择


但我尝试了很多东西,但都不管用。那么这可能吗?如果可能的话,有人能解释一下如何使用一些C代码吗?

您可以在selected index事件中添加一些内容,如果selected index与Precious selected相同,则将其存储在某个位置,然后将selected index设置为-1,因此不选择任何选项。

使用内置选项无法轻松做到这一点。我的解决方案是,当鼠标位于控件上方时,通过编程发送一个虚拟的Ctrl键,这样用户就不需要按下或思考任何事情。如果您不需要MultiExtended的附加功能,请尝试使用MultiSimple

如果你这样做了,下面是一个丑陋的解决方案:

    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    public const byte KEYEVENTF_KEYUP = 0x02;
    public const int VK_CONTROL = 0x11;

    private void listBox1_MouseEnter(object sender, EventArgs e)
    {
        keybd_event(VK_CONTROL, (byte)0, 0, 0);
    }

    private void listBox1_MouseLeave(object sender, EventArgs e)
    {
        keybd_event(VK_CONTROL, (byte)0, KEYEVENTF_KEYUP, 0);
    }
根据我的回答。

遵守SelectedValueChanged事件并添加以下内容:

string selected = null;

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
    ListBox lb = sender as ListBox;
    if (lb == null) { return; }
    if (lb.SelectedItem != null && lb.SelectedItem.ToString() == selected)
    {
        selected = lb.SelectedItem.ToString();
        lb.SetSelected(lb.SelectedIndex, false);
    }
    else 
    {
        selected = lb.SelectedItem == null ? null : lb.SelectedItem.ToString();
    }
}
当您单击ListBox的空白处时,这将取消选择所有/特定的ListBox项目

此外,您还可以在选中/取消选中项目时更改代码

如果您有任何问题,请留下评论。
这是Windows应用程序还是ASP.NET应用程序?您是否考虑过使用多简单模式?我没有使用MultiSimple,因为我认为它没有我想要的行为是的,那么简单,但这正是我想要的thnx。@NominSim您不需要按Ctrl键。虚拟按键是按程序进行的。哦,我现在明白了,你说的很难看是对的,但无论如何+1。这个答案值得两次投票。嗯,你回答了两次,所以你得到了两张赞成票!!
        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int totalHeight = listBox1.ItemHeight * listBox1.Items.Count;

            if(e.Y < totalHeight && e.Y >= 0)
            {
                // Item is Selected which user clicked.

                if(listBox1.SelectedIndex == 0 && listBox1.SelectedItem != null) // Check if Selected Item is NOT NULL.
                {
                    MessageBox.Show("Selected Index : " + listBox1.SelectedItem.ToString().Trim());
                }
                else
                {
                    listBox1.SelectedIndex = -1;
                    MessageBox.Show("Selected Index : No Items Found");
                }
            }
            else
            {
                // All items are Unselected.
                listBox1.SelectedIndex = -1;
                MessageBox.Show("Selected Index : " + listBox1.SelectedItem); // Do NOT use 'listBox1.SelectedItem.ToString().Trim()' here.
            }
        }