WinForms ListBox Append Selection

2019-08-31 08:36发布

问题:

I have a ListBox with SelectionMode = MultiExtended. I want the default behavior for the ListBox to be "append". In other words, the behavior you get when holding down the control key should be the default, passive functionality for the ListBox.

How would I do this? Do I need to subscribe to the "Mouse Down" and "Key Down" events manually? Is there a setting I'm missing?

Thanks.

回答1:

Ugly solution but the best I could do.

    [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);
    }


回答2:

Use MultiSimple Mode

http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode(v=vs.80).aspx

SelectionMode = SelectionMode.MultiSimple