只读(但可放置)组合框
我希望构建一个具有正常颜色的 Windows 窗体组合框,并允许显示下拉列表,但不允许实际更改值。据我所知,这不是 How to make Combobox in 的重复winforms readonly 因为所有建议似乎都是针对禁用组合框的交互性。
我的理由:我有一个表单,其中所有控件都是只读的,并且由于应用程序的性质,我认为当组合框的值不改变时,用户不会感到困惑。我希望用户能够看到组合框绑定的枚举的所有可能值。
到目前为止,我遇到的是一个非常糟糕的黑客:
public partial class ReadOnlyComboBox : ComboBox
{
int prevIndex = -1;
public ReadOnlyComboBox()
{
InitializeComponent();
}
private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (prevIndex <= 0)
prevIndex = SelectedIndex;
else
SelectedIndex = prevIndex;
}
}
实际上,这忽略了框架中的虚假“0”值,并采用从绑定源获取的第一个非零值。直接的缺点是该值只能设置一次,并且绑定枚举必须从 1 开始。
欢迎任何有关清理此问题的建议。谢谢。
I'm looking to build a Windows Forms combo box that has normal coloration, and allows the dropdown list to appear, but does not allow the value to actually change. As far as I can tell, this is not a duplicate of How to make Combobox in winforms readonly since all advice there seems to be directed toward disabling the combo box's interactivity.
My rationale: I have a form where all of the controls are read-only, and due to the nature of the application I think that there would be no risk of the user getting confused when the combo box's value does not change. I would like the user to be able to see all of the possible values of the enum to which the combo box is bound.
What I have so far is a pretty bad hack:
public partial class ReadOnlyComboBox : ComboBox
{
int prevIndex = -1;
public ReadOnlyComboBox()
{
InitializeComponent();
}
private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (prevIndex <= 0)
prevIndex = SelectedIndex;
else
SelectedIndex = prevIndex;
}
}
In effect, this ignores spurious "0" values from the framework, and takes the first non-zero value acquired from a binding source. Immediate disadvantages are that the value may only be set once, and that the bound enum must start at 1.
Any advice on cleaning this up would be welcome. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 DropDownClosed 事件
Use DropDownClosed event