C# Do not frun Textbox TextChanged Event onLoad

Hi Experts

I have an Textbox on which I need to fire a Function on the "TextChanged"-Event. But I do not want to run the TextChanged Event on Loading (Initiaizing) the Form.

How can I do that?
Thanks for your help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

add the delegate to event textchanged while loading the form.

Let me know if you need more clarity.
biko007Author Commented:
Yes please I need more clarity. Thank You!
but before that, I have created the following code but as expected this doesnt call eventhandler while loading (initializing)....have you done something else....I mean what you are facing is not the deafult behavior...Am I missing something?
public partial class Form1 : Form
        public Form1()

        private void textBox1_TextChanged(object sender, EventArgs e)

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

eitherways, what I told you in my previous post is like this
function A Calls Function B which in turn calls function C.
if you dont want the eventhandler to be called while the executing function C wht you can do is:
function A
  call Function B

function B
 this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
call  Function C
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

Hope it helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
let me know if you are stuck somewhere....i will be leaving for the day in next 30 minutes.

to know more about the events and delegates you can check out the following:


anyoneisSoftware DeveloperCommented:
Architecturaly, this problem of events firing and running code at times when I don't
want it is indicative of a tightly coupled and high-maintenance design. That said, I
do it all the time, bacause I haven't had time to "do it right."

I have developed a Blocker class, which lets me use "using" statements to block event execution
when I want to. This gives me a fairly simple and robust mechanism. The code for the class is attached.
When settting SelectedIndex, Text, or some other windows control property which will
generate an event, I use something like this code, where I am setting SelectedIndex on a ComboBox,

          using (new Blocker(worksheetComboBox, true)) {
              // the event the next statement might cause is blocked
              worksheetComboBox.SelectedIndex = -1;
              worksheetComboBox.DisplayMember = "TABLE_NAME";
              worksheetComboBox.DataSource = workbookBindingSource;
              int lastWorksheetIndex = PrefixMatchedNumberImporter.Properties.Settings.Default.LastWorksheetIndex;
              if (lastWorksheetIndex >= 0 && lastWorksheetIndex < workbookBindingSource.Count) {
                  newSelectedIndex = lastWorksheetIndex;
              } else if (workbookBindingSource.Count == 0) {
                  MessageBox.Show("Can't read the spreadsheet");
                  return false;
          // the event the next statement will cause is not blocked
          worksheetComboBox.SelectedIndex = newSelectedIndex;

Then, I condition execution of the event using something like this:
        private void worksheetComboBox_SelectedIndexChanged(object sender, EventArgs e)
            if (!Blocker.Blocked(sender)) {
                int index = worksheetComboBox.SelectedIndex;
                if (index >= 0) {
                    PrefixMatchedNumberImporter.Properties.Settings.Default.LastWorksheetIndex = index;
                    this.Cursor = Cursors.WaitCursor;
                    this.Cursor = Cursors.Default;

This is a recent piece of code, so feedback would be appreciated.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace PrefixMatchedNumberImporter
    // This class is not suitable for multi-threaded operation. It is desinged merely to 
    // stand in for global variables of the type "_initializingSomeContrl" so that the 
    // background code can change the state of a control without worrying about events being fired.
    // It is also handy for FileWatcher events. It is designed to be used in a using block.
    public class Blocker : IDisposable
        // class variables
        private static Dictionary<object, bool> _blocks = new Dictionary<object, bool>();
        // instance variables
        private object _theObject;
        private bool _singleUse;

        public Blocker(object theObject) 
            : this(theObject, false)
        { }

        public Blocker(object theObject, bool singleUse)
            _theObject = theObject;

            if (_blocks.ContainsKey(theObject)) {
                if (singleUse == true) {
                    throw new ApplicationException("Blocker Logic Error");
                _blocks[theObject] = true;
            } else {
                _blocks.Add(theObject, true);
            _singleUse = singleUse;

        public static bool Blocked(object theObject)
            if (_blocks.ContainsKey(theObject)) {
                return _blocks[theObject];
            return false;

        #region IDisposable Members
        public void Dispose()
            if (_blocks.ContainsKey(_theObject)) {
                _blocks[_theObject] = false;
                if (_singleUse) {


Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.