1

I'm a bit new to windows forms so I apologize if there is a simple solution, but I can't find anything on google. If I drag an item, for instance a textbox, onto the form, and double click it, the textbox_TextChanged function will be created for me. This works as expected. However, if I attempted to add a new function such as textbox_Click, it will never be called. In another project, I attempted to add a textbox_Validating function and it isn't being called either (I made sure validating was on in the properties).

Does anyone know why only the TextChanged function is being called?

Here is what is working:

       private void textBox2_TextChanged(object sender, EventArgs e)
    {
        textBox2.BackColor = activeColor;
    } 

Here is what isn't working:

private void textBox2_Click(object sender, EventArgs e)
    {
        textBox2.BackColor = activeColor;
    }

I've set a breakpoint in the textBox2_Click method and it is never being called. I've looked around the web and tried other methods such as _LeftMouseClick and _LeftMouseButtonDown but they aren't working ether.

This also isn't working:

    protected void tbNewPassword_Validating(object sender, CancelEventArgs e)
    {
        if (tbNewPassword.Text.Length < 6)
            epErrorProvider.SetError(tbNewPassword, "Your password must be 6 characters or longer.");
    }

As with the _Click method, I've set a breakpoint and it is never being called.

user1287523
  • 967
  • 3
  • 14
  • 31

2 Answers2

4

select the textbox, open the property window, at the top, there is a small icon that looks like a lightning (there are some other icons at the top, 1 looks like 'A to Z'), click it, it will show all the events. Scroll down, validating is at the end. Input the validating function name, hit return key, it will show validating function in the code. add your code in there.

This helps you add an event.

or, you need add event handler directly, something like

   this.btnIsConnected.Click += new System.EventHandler(this.btnIsConnected_Click);
urlreader
  • 6,319
  • 7
  • 57
  • 91
  • Hah I figured it was something that simple. Thanks. – user1287523 Oct 11 '12 at 05:36
  • This is accurate. Basically you have to tell that control what to do when it's clicked. If you're interested in doing this by code, see: http://msdn.microsoft.com/en-us/library/ms743596.aspx Looking at the code might help to understand what's happening, even if you opt to use the Design view method explained above. – CptSupermrkt Oct 11 '12 at 05:36
  • I don't understand why EVERY tutorial, example, or code snippet doesn't explain this as the first step!! Thank you so much! – Wes Oct 20 '13 at 21:30
0

You have created an event with a valid inner code but it does not get called. why? because it is not attached to your control (textbox in this case), for attaching an event handler to a control you can check this post. And for getting familiar with events at all @urlreader's post is useful.

Community
  • 1
  • 1
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94