如何区分 WinForms CheckBox 中的用户更改与程序更改?
我对 CheckBox 的 OnCheckedChanged 事件有逻辑,该事件在表单加载时以及用户更改检查状态时触发。我希望逻辑仅在用户操作时执行。
是否有一种不依赖于设置/检查用户变量的巧妙方法来检测用户与程序更改?
I have logic on a CheckBox's OnCheckedChanged event that fires on form load as well as when user changes check state. I want the logic to only execute upon user action.
Is there a slick way of detecting user vs programmatic change that doesn't rely on setting/checking user variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的表单上通常有一个 bool 标志,在以编程方式更改值之前将其设置为 true。然后,事件处理程序可以检查该标志以查看它是用户标志还是编程标志。
I usually have a bool flag on my form that I set to true before programmatically changing values. Then the event handler can check that flag to see if it is a user or programmatic.
尝试一些好的旧反思?
调用堆栈是这样的:
所以你需要返回 3 来区分谁 SET Checked
但请记住,有一些东西可能会扰乱调用堆栈,它不是 100% 可靠,但你可以稍微扩展一下来寻找原始来源。
Try some good old reflection?
The Call Stack Goes like this:
So you need to go back 3 to differentiate who SET Checked
Do remember though, there's some stuff that can mess with the call stack, it's not 100% reliable, but you can extend this a bit to search for the originating source.
我已经尝试过并且有效。
I have tried this and it worked.