Denis
Denis

Reputation: 12077

How to find out if the "TextChanged" event is fired if user is typing into a textbox or myTextBox.Text is called programmatically

Is there a way to find out if the "TextChanged" event is fired because

  1. the user is typing into a textbox or
  2. the programmer called myTextBox.Text = "something"?

Just to give you some color on this, I don't want to react when the user is typing each letter into the textbox so I am using the "Validated" event to catch when the user is done so I can react. The problem is I don't have a way to catch when the programmer does "myTextbox.Text = "something". The only way I know to catch changes there is to use TextChanged but then I don't want to be reacting when the user is typing each letter into the textbox. Any suggestions?

Upvotes: 8

Views: 4978

Answers (4)

Joshua Honig
Joshua Honig

Reputation: 13215

I will guess that you're creaing a UserControl that other developers will use, thus "end-user" programmers may set the text programmatically. I think the simplest thing would be to follow @jzworkman's suggestion and make a class that overrides the Text property setter. As @vulkanino notes, you should probably raise and catch the Validating event, though.

public class TextBoxPlus : TextBox {
    public event CancelEventHandler ProgrammerChangedText;
    protected void OnProgrammerChangedText(CancelEventArgs e) {
        CancelEventHandler handler = ProgrammerChangedText;
        if (handler != null) { handler(this, e); }
    }

    public override string Text {
        get {
            return base.Text;
        }
        set {
            string oldtext = base.Text;
            base.Text = value;
            CancelEventArgs e = new CancelEventArgs();
            OnProgrammerChangedText(e);
            if (e.Cancel) base.Text = oldtext;
        }
    }
}

In your source, add the same handler to both the Validating and ProgrammerChangedText events:

// Somewhere...
textBoxPlus1.Validating += textBoxPlus1_Validating;
textBoxPlus1.ProgrammerChangedText += textBoxPlus1_Validating;

void textBoxPlus1_Validating(object sender, CancelEventArgs e) {
    decimal d;
    if (!Decimal.TryParse(textBoxPlus1.Text, out d)) {
        e.Cancel = true;
    }
}

Upvotes: 6

jzworkman
jzworkman

Reputation: 2703

So in your "Formatted" Textbox class:

public override String Text{
   get{return text;}
   set
   {
      //perform validation/formatting
      this.text = formattedValue;
   }

this should allow you to format the text when it is changed by a programmer, the user input validation will still need to be handled in the validating event.

Upvotes: 8

NET Experts
NET Experts

Reputation: 1536

// This is the manual way, which is an alternative to the first way.
// Type 'this.TextChanged += ' into your form's constructor.
// Then press TAB twice, and you will have a new event handler.
this.TextChanged += new EventHandler(textBox1_TextChanged);

void textBox1_TextChanged(object sender, EventArgs e)
{
    //put the logic here for your validation/requirements for validation
    // ex.  if (textbox1.Text=="something") {//don't validate}
    //
}

Upvotes: -3

vulkanino
vulkanino

Reputation: 9124

If you want to perform validation, use the Validating event, not the Validated (which comes when it is too late to act).

That said, what's the real need here?

The Validating event is supplied an object of type CancelEventArgs. If you determine that the control's data is not valid, you can cancel the Validating event by setting this object's Cancel property to true. If you do not set the Cancel property, Windows Forms will assume that validation succeeded for that control, and raise the Validated event.

(http://msdn.microsoft.com/en-us/library/ms229603.aspx)

Upvotes: -2

Related Questions