daniyalahmad
daniyalahmad

Reputation: 3843

Controling if condition in text change function

I want if a user type the character "a" in textbox, message box with message "Ok" displays, and then textbox should clear.But I am facing the problem that when I type "a" message "Ok" is displays and along with message "No" is also displays.But when I remove the statment of clear then all goes good. Please tell me how to overcome this problem ?

private void textBox1_TextChanged(object sender, EventArgs e)      
{
    if (textBox1.Text == "a")
    {
        textBox1.Text = "";
        MessageBox.Show("Ok");
    }
    else
    {
         MessageBox.Show("No");
    }
}

Upvotes: 0

Views: 1670

Answers (6)

arpl
arpl

Reputation: 3633

That's because the textBox1.Text = "" calls textBox1_TextChanged again.

Upvotes: 1

Iswanto San
Iswanto San

Reputation: 18569

You should handle KeyUp event

        public Form1()
        {
            InitializeComponent();
            textBox1.KeyUp+=new KeyEventHandler(textBox1_KeyUp);
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (textBox1.Text == "a")
            {
                textBox1.Text = "";
                MessageBox.Show("Ok");
            }
            else
            {
                MessageBox.Show("No");
            }
        }

Upvotes: 1

Vahid Nateghi
Vahid Nateghi

Reputation: 584

You're using the TextChanged event of the textbox. When you change the text manually the TextChanged event runs again and this time the else expression runs.

private void textBox1_TextChanged(object sender, EventArgs e)      
{
  if(textBox1.Text != "")
    if (textBox1.Text == "a")
    {
        textBox1.Text = "";
        MessageBox.Show("Ok");
    }
    else
    {
         MessageBox.Show("No");
    }
}

Upvotes: 2

nvoigt
nvoigt

Reputation: 77294

You need to check if the change in textBox1.Text was from the user, or from you. "No" is displayed because you change the text to something ("") that is not "a". You could keep a boolean flag that indicates whether you want to react on the change:

bool changedByCode = false;

private void textBox1_TextChanged(object sender, EventArgs e)      
{
    if(changedByCode) return;

    if (textBox1.Text == "a")
    {
        changedByCode = true;
        textBox1.Text = "";
        changedByCode = false;
        MessageBox.Show("Ok");
    }
    else
    {
         MessageBox.Show("No");
    }
}

Upvotes: 1

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

That is because when you set textBox1.Text = ""; the event textBox1_TextChanged is raised one more and there is not a letter 'a' in the textbox and therefore a messasge box with "No" is also displayed.

Upvotes: 1

King King
King King

Reputation: 63317

It's because the Text="" triggered the TextChanged one more time. Use some flag like this:

bool suppressTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e) {
  if(suppressTextChanged) return;
  if (textBox1.Text == "a") {
    suppressTextChanged = true;
    textBox1.Text = "";
    suppressTextChanged = false; 
    MessageBox.Show("Ok");
  } else {
     MessageBox.Show("No");
  }
}

NOTE: The code above supposes you want to check against a string ("a" is just an example). If you want to check against a character. Use the KeyPress event instead.

Upvotes: 3

Related Questions