locked
Button Click not Working After Validating Textbox RRS feed

  • Question

  • Dear All

    I have a simple form with 1 textbox + 1 button.

    When "inside" the textbox, I click the button and this happens:

    1. textbox validates (ok)

    2. returns to form (??)

    Why is Button_Click not being called ?

     

    namespace DoesButtonValidateText
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
          double num;
          if (Double.TryParse(textBox1.Text, out num) == false || num < 0)
          {
            MessageBox.Show("Enter a number >= 0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox1.SelectAll();
            e.Cancel = true;
          }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          MessageBox.Show("Button work");
        }
    
      }
    }

    Friday, May 7, 2010 5:18 PM

Answers

  • Solved !

    I think there is a bug in the debugger !  This problem occurs when the button is clicked and cursor is "inside" the textbox.

    If you add a breakpoint inside:

    textBox1_Validating()

    the program skips:

    button1_Click()

    and returns to the main form.

    If you remove the breakpoint from textBox1_Validating(), after validating the text it fires button1_Click() as expected !

    Took a long to figure this out.  Can anyone tell me why this occurs ?


    • Proposed as answer by Levi Domingos Friday, May 7, 2010 7:31 PM
    • Marked as answer by fabfabfab Friday, May 7, 2010 10:51 PM
    Friday, May 7, 2010 7:03 PM

All replies

  • Do you have a handler on the button? this.button1_click += new Eventhandler....

    If so, does the button click not fire if the validation passes, if the validation fails or never fires?

    Friday, May 7, 2010 6:22 PM
  • Solved !

    I think there is a bug in the debugger !  This problem occurs when the button is clicked and cursor is "inside" the textbox.

    If you add a breakpoint inside:

    textBox1_Validating()

    the program skips:

    button1_Click()

    and returns to the main form.

    If you remove the breakpoint from textBox1_Validating(), after validating the text it fires button1_Click() as expected !

    Took a long to figure this out.  Can anyone tell me why this occurs ?


    • Proposed as answer by Levi Domingos Friday, May 7, 2010 7:31 PM
    • Marked as answer by fabfabfab Friday, May 7, 2010 10:51 PM
    Friday, May 7, 2010 7:03 PM
  • The problem only occurs when a breakpoint is set in text validate().  i.e., the textbox validates, the button method is "skipped".

    When there are no breakpoints, everything works perfectly.  i.e., the textbox validates, and the button method is fired.

     

     

     

    Wednesday, May 12, 2010 4:28 PM