Question Show Form Triggering TextBox.TextChanged Event

ksukat

New member
Joined
Mar 8, 2017
Messages
4
Programming Experience
1-3
Visual Studio Professional 2015. C# Winform project.

I have a form that has a textbox control on it. The textbox
control has a textbox_TextChanged event handler.

When I create the form:
testFrm = new TestForm();
testFrm.Disposed += new EventHandler(testFrm_Disposed);
testFrm.Show();

The Weird thing is that the textbox_TextChanged event gets triggered
when the testFrm.Show() statement executes.

Is this expected behavior? Its causing me some grief.

thanks.
 
Show (displaying the form) does not "trigger" TextChanged event, but if you have set a text in designer then the initialization code will change the Text property to that text, this happens when the form is created, from the constructor.
 
Show (displaying the form) does not "trigger" TextChanged event, but if you have set a text in designer then the initialization code will change the Text property to that text, this happens when the form is created, from the constructor.

OK
I knew that. I do not have a text value assigned in the properties. Here is what I observed.
1. I put a debug.writeline statement in the constructor.
2. I put a debug.writeline statement in the textchanged eventhandler.
3. I put a break statement in the textchanged eventhandler.
4. I put a break statement in the function where the form is created and then shown.

When I it gets to the form.show statment, I step over and it jumps to the textchanged eventhandler in the new form.
Can't figure out why this is happening.
 
I can't repro that.
 
That should execute once, and should not cause a TextChange when the form is shown, right ?

The TextChanged event is raised when the value of the Text property changes. If you have bound the Text property of your TextBox to a data source then the Text property value will change if and when that data source contains data, be that when you bind or when you later populate the data source. If you are populating the data source in the form initialisation code then the Text property value will change and the TextChanged event will be raised.
 
OK.
I have textBox3.DataBindings.Add("Text", blockSource, "loccode1") in the form constructor.
That should execute once, and should not cause a TextChange when the form is shown, right ?
The Text of any TextBox at the start of your program is always an empty string, whenever you bind a TextBox to a source it will update the value in the TextBox when the data is first loaded, if the value that's loaded is also an empty string then the value in the TB will not change, however if there's a string with a length greater than 0 then the TB Text property will change from the empty string to a string with a value and the TextChanged event will fire as expected.

As jmc has also explained:
The TextChanged event is raised when the value of the Text property changes. If you have bound the Text property of your TextBox to a data source then the Text property value will change if and when that data source contains data, be that when you bind or when you later populate the data source. If you are populating the data source in the form initialisation code then the Text property value will change and the TextChanged event will be raised.
 
OK.
I eliminated my problem by moving the textBox3 binding and bindingsource build to the load event.

thanks for all the help!
 
Back
Top Bottom