在 C# 中禁用控件接收事件
我有一个对话框,其中包含大量控件。 每个控件都将在加载序列期间填充,并且可能需要一段时间才能完全填充。 同时,我不想允许用户单击任何控件。 换句话说,我想禁用控件接收事件,但我不想禁用控件(因为它可能看起来很奇怪)。此外,我不想定期订阅和取消订阅事件。 有什么方法可以阻止控件短暂监听事件吗?
苏达桑·斯里尼瓦桑
I have a dialog with loads of control in it. Each and evey control will be populated during the loading sequence and it might take a while for it to get completely filled. Mean while, I don't wanna allow the user to click on any of the controls. In other words, I wanna disable the control from receiving the events and I don't wanna disable the controls (as it might look odd).Also, I don't wanna subscribe and unsubscribe for the events regular intervals. Is there any way to stop the controls from listening to the events for a brief time ??
Sudarsan Srinivasan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,我知道的唯一方法是拥有一个类变量(称为 _loading 之类的东西),并在每个控制处理程序中执行以下操作:
一旦完成加载,就在加载代码中设置
_loading = true;
。Unfortunately the only way i know of is to have a class variable (called something like _loading) and in each control handler do something like:
And in your loading code set
_loading = true;
once you have finished loading.如果您只想禁用用户输入,则可以将表单的
Enabled
属性设置为 false。这具有阻止用户对任何表单控件进行输入的效果,而不改变控件的外观; 这是
ShowDialog
方法内部使用的技术。If you just want to disable user input, then you can set the form's
Enabled
property to false.This has the effect of blocking user input to any of the form's controls, without changing the appearance of the controls; it's the technique used internally by the
ShowDialog
method.禁用控件的全部目的是向用户传达该控件在特定时间无法使用的信息。 这是用户已经学习并习惯的约定,因此我建议遵循该约定。 不这样做可能会让用户感到困惑。
最简单的方法是禁用控件所在的容器,而不是禁用每个控件。 更好的方法(或者至少是我更喜欢的方法)是有一种方法可以根据 UI 所处的状态来控制控件的 Visible 和 Enabled 属性。
The whole point of disabling controls is to communicate to the user that the control cannot be used at a particular time. This is a convention that users have learned and are used to, so I would advice to follow that. Not doing that may confuse the users.
The easiest way is to disable the container in which the controls are located in, rather than disabling each and every control. A better way (or at least the way that I prefer) is to have a method that will control the
Visible
andEnabled
properties of controls based on which state the UI is in.最简单的方法是将控制群体移出加载事件(如果可能)。 然后在 Load 中执行类似以下操作:
编辑 其他一些想法:
The easiest way is to move the control population out of the load event (if possible). Then in Load do something like:
Edit A Couple other Ideas: