【问题标题】:C# Gui setting control.Enabled to false fires OnClick event?C# Gui 设置 control.Enabled 为 false 触发 OnClick 事件?
【发布时间】:2010-04-01 00:26:44
【问题描述】:

由于某些非常奇怪的原因,当我在小型 GUI 上的简单文本框上将 .Enabled 属性设置为 false 时,它​​会触发单选按钮 OnClick 事件并导致很多问题。

我已经为 txtBox.Enabled = false; 设置了断点。并且在跨过或进入它之后,我直接跳转到单选按钮控件的 OnClick 事件

这是发生的调用堆栈:

TestGUI.exe!TestGUI.frmMain.radiobuttonClicked(object sender = {Text = "Download Single Episode" Checked = true}, System.EventArgs e = {System.EventArgs}) 第 67 行 C# System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) + 0x70 字节 System.Windows.Forms.dll!System.Windows.Forms.RadioButton.OnClick(System.EventArgs e) + 0x27 字节 System.Windows.Forms.dll!System.Windows.Forms.RadioButton.OnEnter(System.EventArgs e = {System.EventArgs}) + 0x3e 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.NotifyEnter() + 0x20 字节 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl() + 0x195 字节 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {Text = "Download Single Episode" Checked = true}) + 0x54 字节 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control, bool originator = false) + 0x76 bytes System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms.Control value = {Text = "Download Single Episode" Checked = true}) + 0x73 字节 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControl(System.Windows.Forms.Control ctl) + 0x33 字节 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActiveControl.set(System.Windows.Forms.Control 值) + 0x5 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.Select(布尔定向,布尔转发)+ 0x1b 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControl(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x7b 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControlInternal(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x4a 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextIfFocused() + 0x61 字节 System.Windows.Forms.dll!System.Windows.Forms.Control.Enabled.set(bool value) + 0x42 bytes

什么鬼?

这和我订阅事件的方式没有任何关系吧?

this.radioBtnMultipleDownload.Click += radiobuttonClicked; this.radioBtnSingleDownload.Click += radiobuttonClicked; this.radioCustomUrl.Click += radiobuttonClicked;

【问题讨论】:

标签: c# user-interface controls events


【解决方案1】:

调用堆栈的倒数第二行:

System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextIfFocused() + 0x61 字节

显然,RadioButton 会在其OnEnter 上触发OnClicked,而OnEnter 会从其UpdateFocusedControl 触发,这是因为这是下一个控件。您可以尝试在 TextBox 被禁用之前调用 Control.Focus() 来获取您想要获得焦点的其他内容,以便 SelectNextIfFocused() 不会做任何事情,即:

dummyTextBox.Focus();
txtBox.Enabled = false;

【讨论】:

  • 非常好,这确实解决了它。我添加了一个随机文本框,并在执行 .Enabled = false 之前将其重点放在了此处,但是有没有更优雅的解决方案?除了另一个单选按钮之外,我真的没有任何其他控件可以聚焦! >_
  • @Daniel,不是我知道的手...我自己......像这样的事情,我总是有另一个我可以关注的控件(通常是“提交”按钮或其他东西)。
  • 啊,是的,我把 OK 按钮放在了焦点上,这很有魅力,但是有没有办法让按钮失焦?还是有些事情总是需要重点关注?
  • 我认为某些事情总是需要有重点,但我不能 100% 确定它是如何工作的。您也可以使用 TabIndex (这就是确定“下一步”控件的原因),并以这种方式提出更优雅的解决方案。基本上,如果用户在文本框中按 Enter 或 Tab 并且您禁用它,您希望发生什么?
  • 在他们填写了一些信息然后单击“确定”后,我将禁用这些控件,否则他们仍然可以修改条目并使其看起来错误。我已经制作了另一个按钮,无论如何我都打算这样做来停止这个过程,所以我会专注于它,一切都会好的! :) 感谢您的帮助
【解决方案2】:

TextBox 有一个 EnabledChanged 属性。你确定你不是在某个地方这样做吗?

this.textBox1.EnabledChanged += radiobuttonClicked;

另一种可能性是您已经连接到 TextBox 的 LostFocus 事件,如果在您禁用 TextBox 时获得焦点,该事件将触发。或者,您可能已经连接到某个其他控件的 GotFocus 事件,如果在您禁用它时 TextBox 具有焦点,该事件也会触发。

【讨论】:

  • 从堆栈跟踪来看,它与那个事件无关,而是焦点被移动到下一个控件的奇怪副作用,因为它所在的那个被禁用了。
  • 我刚刚为“EnabledChanged”做了一个完整的解决方案 ctrl + f 并没有出现任何结果,所以我猜它是别的东西。这是非常奇怪和令人沮丧的
【解决方案3】:

快速说明 - 不是真正的解决方案,但提供了一些提示:我只是在一个完全静态的对话框中遇到了同样的问题,没有任何 EnabledChanged 处理程序或调用,事实上即使没有任何事件处理程序分配给我的 RadioButtons。并且没有数据绑定或其他黑魔法。

似乎 SelectNextIfFocused 在 ShowDialog 中点击了这些按钮,只是因为它们在控件的选项卡顺序中排在第一位,尽管它们嵌套在 GroupBox 内的 TableLayoutPanel 中。这导致检查了错误的 RadioButton。由于对话框的 tab 顺序是歪斜的,我先改变了它。

这似乎解决了我的问题。但是您也可以尝试类似的方法,即在您的控件中插入一些空标签以使其接收由 SelectNextIfFocused 产生的事件。

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2014-09-18
    • 2010-10-21
    相关资源
    最近更新 更多