Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Textbox text without firing TextChanged event

Tags:

My application in C# has a Textbox with a txt_TextChanged event.

private void txt_TextChanged(object sender, EventArgs e)
{
  //Do somthin
}

But there's one specific part that I want to change txt.Text without firing the txt_TextChanged event.

txt.Text ="somthing" //Don't fire txt_TextChanged

How can I do that?

like image 745
Alex Jolig Avatar asked Jan 04 '15 06:01

Alex Jolig


People also ask

How to disable TextChanged event in c#?

Simpliest way that I can think of is using conditnional bool variable. When you are going to set the text on LostFocus set it to true and inside textChanged event handler check if that bool variable is true , do not do nothing. Show activity on this post.

Which event is generated when TextBox text is changed?

The event handler is called whenever the contents of the TextBox control are changed, either by a user or programmatically. This event fires when the TextBox control is created and initially populated with text.

Which event is fired when there is a change in the content of the text box?

Remarks. The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.


2 Answers

There is no direct way to prevent the raising of events for the text property, however your event handler can use a flag to determine weather or not to perform a task. This i likely to be more efficient than attaching and detaching the event handler. This can be done by a variable within the page or even a specialized class wrapper

With a variable:

skipTextChange = true;
txt.Text = "Something";

protected void TextChangedHandler(object sender, EventArgs e) {
  if(skipTextChange){ return; }
  /// do some stuffl
}

With specialized event handler wrapper

   var eventProxy = new ConditionalEventHandler<EventArgs>(TextBox1_TextChanged);
    TextBox1.TextChanged = eventProxy.EventAction;

    eventProxy.RaiseEvents = false;
    TextBox1.Text = "test";


    public void TextBox1_TextChanged(object sender, EventArgs e) {
       // some cool stuff;
    }

    internal class ConditionalEventHadler<TEventArgs> where TEventArgs : EventArgs
{
   private Action<object,TEventArgs> handler;

    public bool RaiseEvents {get; set;}

    public ConditionalEventHadler(Action<object, TEventArgs> handler)
    {
        this.handler = handler; 
    }

    public void EventHanlder(object sender, TEventArgs e) {
      if(!RaiseEvents) { return;}
      this.handler(sender, e);
    }
}
like image 100
Austin Avatar answered Sep 20 '22 01:09

Austin


txt.TextChanged -= textBox1_TextChanged;  // dettach the event handler
txt.Text = "something"; // update value
txt.TextChanged += textBox1_TextChanged; // reattach the event handler
like image 20
Mr Balanikas Avatar answered Sep 22 '22 01:09

Mr Balanikas