views:

359

answers:

2

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 = "Download 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;

A: 

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.

MusiGenesis
Judging by the stack trace it's not related to that event, but a weird side-effect of the focus being moved to the next control because the one it was on became disabled.
Matti Virkkunen
I just did a entire solution ctrl + f for "EnabledChanged" and nothing came up, so i guess it's something else. This is very weird and frustrating
Daniel
+1  A: 

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;
Tanzelax
Very nice this did fix it. I added a random text box and focused that before i did the .Enabled = false, however is there a more elegant solution? I don't really have any other controls i can focus apart from another radio button! >_<
Daniel
@Daniel, Not that I know of off hand... I've seen a couple people recommend having a non-visible control that you could focus specifically for this, but haven't ever really done that myself... things like this, I've always had another control (usually a 'submit' button or something) that I could focus.
Tanzelax
Ah yep i focused the OK button and that worked a charm, however is there a way to unfocus the button after? Or does something always need to have focus?
Daniel
I think something always needs to have focus, but I'm not 100% sure on the subtleties of how that works. You could play around with TabIndex too (that's what determines the "Next" control), and come up with a more elegant solution that way. Basically, what do you want to happen if the user hits Enter or Tab if they were in the text box and you disable it?
Tanzelax
I'm disabling the controls after they have filled in some information and then clicked OK, otherwise they could still modify the entries and make it look wrong. I have made another button which i was intending on doing anyway to stop the process, so i'll focus that and it'll all be good! :) Thanks for your help
Daniel