Winforms - 如何防止列表框项目选择

tom*_*x66 6 .net c# winforms

在WinForms中,我在一个选择Items的Listbox上运行循环.

在此期间,我不希望用户使用鼠标或键选择该列表框中的项目.

我查看了MyListbox.enabled = false,但它会显示所有项目.不要那样.

如何防止在列表框中选择项目?

Nat*_*han 7

我也想一个只读列表框,最后,经过一番搜索,发现这个从http://ajeethtechnotes.blogspot.com/2009/02/readonly-listbox.html:

public class ReadOnlyListBox : ListBox
{
    private bool _readOnly = false;
    public bool ReadOnly
    {
        get { return _readOnly; }
        set { _readOnly = value; }
    }

    protected override void DefWndProc(ref Message m)
    {
        // If ReadOnly is set to true, then block any messages 
        // to the selection area from the mouse or keyboard. 
        // Let all other messages pass through to the 
        // Windows default implementation of DefWndProc.
        if (!_readOnly || ((m.Msg <= 0x0200 || m.Msg >= 0x020E)
        && (m.Msg <= 0x0100 || m.Msg >= 0x0109)
        && m.Msg != 0x2111
        && m.Msg != 0x87))
        {
            base.DefWndProc(ref m);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Khh*_*Khh 5

Listbox.SelectionMode属性切换到SelectionMode.None

编辑 我看到设置为 SelectionMode.None取消选择所有以前选择的项目,如果在列表框上调用SetSelected,则抛出异常.

我认为不可能实现所需的行为(不想让项目变灰Enabled=false).