kaycee
kaycee

Reputation: 1309

TextBox event for only user input

I have a Textbox control which sometimes updated programmatically and also can be update by the user. How can I distinct between those two event? I'd like to have a "Dirty" flag set to "True" when the user changes the text.

Upvotes: 9

Views: 12643

Answers (5)

Chris Barlow
Chris Barlow

Reputation: 3314

You can use the Key Down event of the text box.

  private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
      // Insert the code you want to run when the text changes here!
  }

Upvotes: 9

Oleksandr V. Kukliuk
Oleksandr V. Kukliuk

Reputation: 211

Check Modified property of TextBox on the TextChanged event. If true, the changes were made by user, otherwise the text was changed programmatically.

Example:

void Texbox_TextChanged(object sender, EventArgs e)
{
    if (((TextBox)sender).Modified)
        TextboxUserInput();
}

Upvotes: 15

naderi
naderi

Reputation: 69

my solution work for type , copy and paste

    private void TextChanged(object sender, EventArgs e)
    {
        if (((TextBox)sender).ContainsFocus)
        {

        }
    }

Upvotes: 6

Radagast the Brown
Radagast the Brown

Reputation: 3366

try onBlur

this will catch the moment the user leaves the field.

You can work it up in conjunction with onFocus to save the value before editing

Upvotes: 0

Dustin Davis
Dustin Davis

Reputation: 14585

avoid setting the "dirty" flag when done programmatically. Disable the event handler or set a flag that says "this is code, not the user"

Upvotes: 4

Related Questions