在 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/