两个控件正确地相互引用
我在 .net 表单应用程序中有一个 ComboBox 和一个 DateTimePicker 控件。两个控件的关系所需的功能是对一个控件的修改将更改另一个控件中的值。修改其他控件的逻辑是在每个控件的更改事件中; ComboBox“SelectedIndexChanged”和 DateTimePicker“Changed”,它看起来如下所示:
othercontrol.value = value;
有没有一种明确的方法可以将更改事件与相应的控件隔离,以确定它们是由 UI 发送还是由其他控件的更改事件发送当前设置会导致什么循环?
当我写这篇文章时,我意识到我可能可以通过调用更改事件并传递相应更改事件的参数中的一些变化来更改其他控件的值,而不是简单地设置其他控件的值。
I have a ComboBox and a DateTimePicker control in a .net forms application. The desired functionality for the relationship of the two controls is that a modification to one will change the value in the other. Logic to modify the other control is in each controls change event; ComboBox "SelectedIndexChanged" and DateTimePicker "Changed" and it looks something like the following:
othercontrol.value = value;
Is there a clear way I can isolate change events from the respective controls to determine whether they were sent by the UI vs. the other control's change event to head off the loop the current setup will cause?
As I write this I realize I could probably change the other controls value by invoking the change event and passing some variation in the arguments from the corresponding change event instead of simply setting the other control's value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在设置其值之前删除控件的事件处理程序,并在设置值后立即将其添加回来。
You can remove the control's eventhandler before settings it's value and add it back immediately after setting the value.
那就试试这个吧。
好吧,这有点 hacky,但这会起作用:
或者,您可以使用枚举来跟踪状态,而不是使用布尔“标志”,但我认为布尔更容易演示。
Try this then.
Ok, it's a bit hacky but this would work:
Alternatively, you could use an Enumeration to track state instead of using boolean 'flags' but I think bools are easier to demonstrate.