WinForms ListBox Append Selection

此生再无相见时 提交于 2019-12-25 01:16:00

问题


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


来源:https://stackoverflow.com/questions/11350514/winforms-listbox-append-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!