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

标签 c# .net winforms

在 WinForms 中,我偶尔会在列表框上运行一个循环来选择项目。

在那段时间里,我不希望用户使用鼠标或按键选择该列表框中的项目。

我查看了 MyListbox.enabled=false,但它使所有项目变灰。不想这样。

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

最佳答案

我也想要一个只读列表框,最后,经过大量搜索,从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);
        }
    }
}

关于c# - Winforms - 如何防止列表框项目选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197923/

相关文章:

.net - 非常快的 .NET 4 嵌入式数据库

c# - 标签数组

c# - 匹配 .html 之前和之后的文本的正则表达式/

winforms - DataGridView 中的总计行

c# - 如何通过 C# 检索所有可能的服务器名称

c# - 使用 TextFieldParser 处理包含未转义双引号的字段

c# - 来自 Windows 服务的屏幕截图

c# - 列标题与 Wpf Datagrid 中数据的偏移

C# 从另一个线程调用 form.show()

c# - 将列表数组中的值绑定(bind)到列表框