Winforms-如何防止选择列表框项目 [英] Winforms - How to prevent Listbox item selection
本文介绍了Winforms-如何防止选择列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WinForms中,我有时会在选择项目的列表框上运行一个循环.
In WinForms I occationally have a loop running over a Listbox selecting Items.
在那段时间里,我不希望用户使用鼠标或键在该列表框中选择项目.
During that time I don't want the user to select items in that listbox with the mouse or keys.
我查看了MyListbox.enabled = false,但它使所有项目均变灰.不想那样.
I looked at MyListbox.enabled=false but it grays out all items. Dont't want that.
如何防止在列表框中选择项目?
How to prevent selecting items in a Listbox?
推荐答案
我也想要一个只读列表框,最后,经过大量搜索,从http://ajeethtechnotes.blogspot.com/2009/02/readonly-listbox.html :
I too wanted a read only list box, and finally, after much searching, found this from 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);
}
}
}
这篇关于Winforms-如何防止选择列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文