3

I am using this on my webform, to create dynamic buttons.

Button b1 = new Button();

I would like to get this:

b1.Click+=new EventHandler(OnClick);

How can I do this? I want that the event is created automatically, it could be done with pressing twice tab or something, but I forgot it...

1
  • Type b1.Click += [TAB] [TAB] and it should be autocreated. Commented Aug 13, 2013 at 13:58

3 Answers 3

5

If you mean you want to know the signature for OnClick it would be:

public void CreateDynamicButtons()
{
    Button b1 = new Button();
    b1.Click += new EventHandler(OnClick);

    // Or you could simply do
    Button b2 = new Button();
    b2.Click += OnClick;
}

protected void OnClick(Object sender, EventArgs e)
{
    // This is called when b1 or b2 are clicked
}
Sign up to request clarification or add additional context in comments.

Comments

1

In my environment (VS2010 Pro or VS2012 Express), after you type the +=, you can press Tab twice to generate the event handler.

2 Comments

Ah, I thought it also created the += aswell after pressing tab.. Thanks :)
Yes, it works the same in both: as long as it's marked event, it should work. Maybe there's an option for it somewhere and you have it disabled?
0

Yes you can do tht by pressing tab twice after += or you can do it manually by

b1.Click+=b1_Click;
private void b1_Click(object sender, EventArgs e)
        {
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.