Having A Spot Of Trouble With Events
-
As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:
private void rbTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbTrap.Checked == true)
{
Shape = myShape.Trap;
MyArgs.MyControl = "rbTrap";
RaiseEvent(rbTrap, MyArgs);
lblDim1.Text = "Depth, d";
lblDim2.Text = "Bottom Width, W1";
lblDim3.Text = "Top Width, W2";
lblDim3.Visible = true;
txtDim3.Visible = true;
Invalidate();
}
}private double ValidateEntry(TextBox MyTextBox) { try { return Double.Parse(MyTextBox.Text); } catch (FormatException ex) { MessageBox.Show("Enter a valid numeric value" + ex.Message); MyTextBox.Focus(); return 0.0; } } //txtDim1 private void txtDim1\_Enter(object sender, EventArgs e) { txtDim1.SelectAll(); } private void txtDim1\_Leave(object sender, EventArgs e) { prev = dim1; //Save the current value dim1 = ValidateEntry(txtDim1); //Get the new value if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed { MyArgs.MyControl = "txtDim1"; RaiseEvent(txtDim1, MyArgs); } } private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) {
-
As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:
private void rbTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbTrap.Checked == true)
{
Shape = myShape.Trap;
MyArgs.MyControl = "rbTrap";
RaiseEvent(rbTrap, MyArgs);
lblDim1.Text = "Depth, d";
lblDim2.Text = "Bottom Width, W1";
lblDim3.Text = "Top Width, W2";
lblDim3.Visible = true;
txtDim3.Visible = true;
Invalidate();
}
}private double ValidateEntry(TextBox MyTextBox) { try { return Double.Parse(MyTextBox.Text); } catch (FormatException ex) { MessageBox.Show("Enter a valid numeric value" + ex.Message); MyTextBox.Focus(); return 0.0; } } //txtDim1 private void txtDim1\_Enter(object sender, EventArgs e) { txtDim1.SelectAll(); } private void txtDim1\_Leave(object sender, EventArgs e) { prev = dim1; //Save the current value dim1 = ValidateEntry(txtDim1); //Get the new value if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed { MyArgs.MyControl = "txtDim1"; RaiseEvent(txtDim1, MyArgs); } } private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) {
Hi Roger. I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-
double prev; double dim1; private void txtDim1\_Leave(object sender, EventArgs e) { prev = dim1; dim1 = ValidateEntry(txtDim1); if (dim1 != 0.0 & prev != dim1) { MessageBox.Show("Event Fired"); } } private double ValidateEntry(TextBox MyTextBox) { try { return Double.Parse(MyTextBox.Text); } catch (FormatException ex) { MessageBox.Show("Enter a valid numeric value" + ex.Message); MyTextBox.Focus(); return 0.0; } } private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { txtDim2.Focus(); } }
and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.
The FoZ
-
As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:
private void rbTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbTrap.Checked == true)
{
Shape = myShape.Trap;
MyArgs.MyControl = "rbTrap";
RaiseEvent(rbTrap, MyArgs);
lblDim1.Text = "Depth, d";
lblDim2.Text = "Bottom Width, W1";
lblDim3.Text = "Top Width, W2";
lblDim3.Visible = true;
txtDim3.Visible = true;
Invalidate();
}
}private double ValidateEntry(TextBox MyTextBox) { try { return Double.Parse(MyTextBox.Text); } catch (FormatException ex) { MessageBox.Show("Enter a valid numeric value" + ex.Message); MyTextBox.Focus(); return 0.0; } } //txtDim1 private void txtDim1\_Enter(object sender, EventArgs e) { txtDim1.SelectAll(); } private void txtDim1\_Leave(object sender, EventArgs e) { prev = dim1; //Save the current value dim1 = ValidateEntry(txtDim1); //Get the new value if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed { MyArgs.MyControl = "txtDim1"; RaiseEvent(txtDim1, MyArgs); } } private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) {
Hi Roger, Thanks for the kind words :rose: My only initial thought is to set a breakpoint inside
if (rbTrap.Checked == true)
. I have had wierd behaviour before withCheckBox
es/RadioButton
s whereCheckedChanged
has raised more often than expected. If that doesn't help isolate the cause then feel free to drop me an email through the link at the bottom of this post and I'll happily take a look at the project for you and root the b**ger out!Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi Roger. I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-
double prev; double dim1; private void txtDim1\_Leave(object sender, EventArgs e) { prev = dim1; dim1 = ValidateEntry(txtDim1); if (dim1 != 0.0 & prev != dim1) { MessageBox.Show("Event Fired"); } } private double ValidateEntry(TextBox MyTextBox) { try { return Double.Parse(MyTextBox.Text); } catch (FormatException ex) { MessageBox.Show("Enter a valid numeric value" + ex.Message); MyTextBox.Focus(); return 0.0; } } private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { txtDim2.Focus(); } }
and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.
The FoZ
I think Dave may be on to something; it has to do with the radio button somehow. The extra event does not occur if I use the mouse to move to another text box, only when I move to a radio button. The messages displayed also show that the text did not change, but it does happen that the text changes from Selected to unselected when the focus is moved to the radio button. I wonder if that's being picked up as a text change and firing the event?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"