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.
biko007Asked:
Who is Participating?
 
lucky_jamesCommented:
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.
0
 
lucky_jamesCommented:
add the delegate to event textchanged while loading the form.

Let me know if you need more clarity.
0
 
biko007Author Commented:
Yes please I need more clarity. Thank You!
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
lucky_jamesCommented:
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()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("here");
        }
    }

Open in new window

0
 
lucky_jamesCommented:
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:
http://msdn.microsoft.com/en-us/library/t3d01ft1(v=VS.80).aspx

http://www.c-sharpcorner.com/UploadFile/anandnarayanswamy/EventHandlinginCSharp11282005012049AM/EventHandlinginCSharp.aspx




0
 
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,
worksheetComboBox:

          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;
                    LoadWorksheet();
                    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()
        {
            Debug.Assert(_blocks.ContainsKey(_theObject));
            if (_blocks.ContainsKey(_theObject)) {
                _blocks[_theObject] = false;
                if (_singleUse) {
                    _blocks.Remove(_theObject);
                }
            }
        }
        #endregion
    }

}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.