两个控件正确地相互引用

发布于 2024-11-05 14:56:04 字数 384 浏览 0 评论 0原文

我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-11-12 14:56:04

您可以在设置其值之前删除控件的事件处理程序,并在设置值后立即将其添加回来。

othercontrol.SelectedChanged -= othercontrol_SelectedChanged;
othercontrol.value = value;
othercontrol.SelectedChanged += othercontrol_SelectedChanged;

You can remove the control's eventhandler before settings it's value and add it back immediately after setting the value.

othercontrol.SelectedChanged -= othercontrol_SelectedChanged;
othercontrol.value = value;
othercontrol.SelectedChanged += othercontrol_SelectedChanged;
凹づ凸ル 2024-11-12 14:56:04

那就试试这个吧。

好吧,这有点 hacky,但这会起作用:

private bool eventBubbledDate = false;
private bool eventBubbleCombi = false;

protected override MyCombi_OnChange(object sender, eventargs e)
{
  if (eventBubbledDate)
  {
    eventBubbledDate = false;
    return;
  }
  eventBubbleCombi = true;
  myDateTime.Value = myCombi.SelectedValue;
}

protected override MyDateTime_OnChange(object sender, eventargs e)
{
  if (eventBubbleCombi )
  {
     eventBubbleCombi = false;
     return;
  }

  // process DateStuff here
  // update other control
  eventBubbledDate = true;
}

或者,您可以使用枚举来跟踪状态,而不是使用布尔“标志”,但我认为布尔更容易演示。

Try this then.

Ok, it's a bit hacky but this would work:

private bool eventBubbledDate = false;
private bool eventBubbleCombi = false;

protected override MyCombi_OnChange(object sender, eventargs e)
{
  if (eventBubbledDate)
  {
    eventBubbledDate = false;
    return;
  }
  eventBubbleCombi = true;
  myDateTime.Value = myCombi.SelectedValue;
}

protected override MyDateTime_OnChange(object sender, eventargs e)
{
  if (eventBubbleCombi )
  {
     eventBubbleCombi = false;
     return;
  }

  // process DateStuff here
  // update other control
  eventBubbledDate = true;
}

Alternatively, you could use an Enumeration to track state instead of using boolean 'flags' but I think bools are easier to demonstrate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文