C# Gui setting control.Enabled to false fires OnClick event?
For some really weird reason when i set the .Enabled property to false on a simple text box on a small GUI, it fires a radio buttons OnClick event and its causing lots of problems.
I have breakpointed the txtBox.Enabled = false; and after stepping over OR into it i jump straight to the OnClick event of the radio button control
Here is the call stack as that happened:
TestGUI.exe!TestGUI.frmMain.radiobuttonClicked(object sender = {Text = "Download Single Episode" Checked = true}, System.EventArgs e = {System.EventArgs}) Line 67 C# System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) + 0x70 bytes System.Windows.Forms.dll!System.Windows.Forms.RadioButton.OnClick(System.EventArgs e) + 0x27 bytes System.Windows.Forms.dll!System.Windows.Forms.RadioButton.OnEnter(System.EventArgs e = {System.EventArgs}) + 0x3e bytes System.Windows.Forms.dll!System.Windows.Forms.Control.NotifyEnter() + 0x20 bytes System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl() + 0x195 bytes System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {Text = "Downloa开发者_StackOverflow社区d Single Episode" Checked = true}) + 0x54 bytes 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 bytes System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControl(System.Windows.Forms.Control ctl) + 0x33 bytes System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActiveControl.set(System.Windows.Forms.Control value) + 0x5 bytes System.Windows.Forms.dll!System.Windows.Forms.Control.Select(bool directed, bool forward) + 0x1b bytes System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControl(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x7b bytes System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControlInternal(System.Windows.Forms.Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) + 0x4a bytes System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextIfFocused() + 0x61 bytes System.Windows.Forms.dll!System.Windows.Forms.Control.Enabled.set(bool value) + 0x42 bytes
What the hell?
It wouldn't have anything to do with the way i subscribe to the events would it?
this.radioBtnMultipleDownload.Click += radiobuttonClicked;
this.radioBtnSingleDownload.Click += radiobuttonClicked;
this.radioCustomUrl.Click += radiobuttonClicked;
Second to last line of your call stack:
System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextIfFocused() + 0x61 bytes
Apparently, RadioButton
fires OnClicked
on its OnEnter
, which fires from its UpdateFocusedControl
, which happens because this is the next control. You could try to call Control.Focus()
on something else that you want to gain focus before the TextBox
is disabled, so that SelectNextIfFocused()
won't do anything, ie:
dummyTextBox.Focus();
txtBox.Enabled = false;
TextBox
has an EnabledChanged
property. Are you sure you're not doing this somewhere?
this.textBox1.EnabledChanged += radiobuttonClicked;
Another possibility is that you've hooked into the TextBox's LostFocus
event, which will fire if the TextBox has the focus when you disable it. Or maybe you've hooked into some other control's GotFocus
event, which would also fire if the TextBox has the focus when you disable it.
Quick note - not really a solution, but providing some hints: I just ran into the same problem in a quite static dialog without any EnabledChanged handlers or calls, in fact even without any event handlers assigned to my RadioButtons. And no data binding or other black magic.
It seems that SelectNextIfFocused hit those buttons inside ShowDialog merely because they were first in the control's tab order, albeit nested inside a TableLayoutPanel inside a GroupBox. This resulted in the wrong RadioButton being checked. And since the dialog's tab order was askew, I changed that first.
This seems to have fixed the problem for me. But you may as well try something similar, i.e. insert some empty label among your controls to have it receive the events resulting from SelectNextIfFocused.
精彩评论