WinForms: two way TextBox problem

I have a TextBox, which I use for both data input and display. A user can select items from a list and for each item there is a corresponding text which is displayed in the TextBox when the item is selected.

The user can edit this textbox, thus changing the list item's correponding text.

My problem is this: I use the TextChanged event to detect when the user enters some text and I update the internal variables in the event handler, however this event handler is called when I programmatically change the values of the textbox too. I want this handler only to be called when the User changes the textbox. How can I achieve this?

<strong>edit:</strong> I have the same problem a combo box as well.


You can use the <strong>Latch</strong> pattern: see Rein in runaway events with the "Latch"


The handler will always be called - no way disable it that I'm aware of. However you can simply set a flag in your class to indicate the you've programatically changed the state and should ignore the next event.

_updating = true; _textBox.Text = "New Text"; ... _textBox_TextChanged( object sender, EventArgs e ) { if( _updating ) { _updating = false; return; } // Do something special with the new text. }

You might also try creating your textbox control and overriding the Text property to provide your own custom logic.

class MyTextBox : TextBox { public overrides string Text{ get{ return base.Text; } set{ if( value == Text ) return; _updating = true; base.Text = value; _updating = false; } } }


The TextChanged event is invoked whenever the TextBox changes its Text property, which can be achieved via modifying the Text property, or when the user directly changes the text, there are other events which are more likely usable for the scenario you are trying to achieve.

    <li>KeyPress: Raised whenever the user pulses a key</li> <li>KeyDown: Raised whenever a Key is pulled down</li> <li>KeyUp: Raised whenever a Key is pulled up (released)</li> </ul>

    Hope it helps


    You could set a flag in the programatic updates, and in the event handler exit early if the flag is set.

    But if you use databinding, then the variables and the gui are synchronized automatically, which avoids this problem all together.


    Do you mean you want the second textbox to display something which depends on what's in the first textbox, but ONLY if the user did set it ?

    Could you be more specific about what you are trying to achieve? It seems to me that your UI design may be confusing for the user, since the text in B is sometimes related to A, and sometimes not.

    If you still want to do it, the typical way to go is to use an updating flag, as proposed in other answers.


    Check for focus on the textbox. If it does not have focus, it means the user isn't editing it.


  • How to shutdown a GenericKeyedObjectPool safely?
  • javafx in swing exception “Toolkit not initialized”
  • VHDL Gated Clock how to avoid
  • Java multithreading - awaiting condition and releasing all threads
  • Disabling sound of embedded flash object with html
  • Cut the background to expose the layer below
  • KnockoutObservableArray with typed elements in TypeScript
  • OSX - always hide certain files
  • ThreadStatic in asynchronous ASP.NET Web API
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Tamper-proof configuration files in .NET?
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Ensure fsync did its job
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • How to set ini file attributes during an Inno install
  • Row Count Is Returning the incorrect number using RaptureXML
  • Yii2: Config params vs. const/define
  • Python CGI os.system causing malformed header
  • NetLogo BehaviorSpace - Measure runs using reporters
  • jQuery show() function is not executed in Safari if submit handler returns true
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • javascript inside java/jsp code
  • Convert array of 8 bytes to signed long in C++
  • R: gsub and capture
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • How to disable jQuery.jplayer autoplay?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Understanding cpu registers
  • how does django model after text[] in postgresql [duplicate]
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF