Results 1 to 8 of 8

Thread: Show Form Triggering TextBox.TextChanged Event

  1. #1
    ksukat is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Show Form Triggering TextBox.TextChanged Event

    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.

  2. #2
    JohnH's Avatar
    JohnH is offline C# Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    331
    Rep Power
    87
    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.

  3. #3
    ksukat is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0
    Quote Originally Posted by JohnH View Post
    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.

  4. #4
    JohnH's Avatar
    JohnH is offline C# Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    331
    Rep Power
    87
    I can't repro that.

  5. #5
    ksukat is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0
    Quote Originally Posted by JohnH View Post
    I can't repro that.
    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 ?

  6. #6
    jmcilhinney is online now C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,435
    Rep Power
    116
    Quote Originally Posted by ksukat View Post
    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.

  7. #7
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Location
    Lansing, MI; USA
    Posts
    85
    Rep Power
    76
    Quote Originally Posted by ksukat View Post
    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:
    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    ksukat is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0
    OK.
    I eliminated my problem by moving the textBox3 binding and bindingsource build to the load event.

    thanks for all the help!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •