0

I am using Tabcontrol and I created a class for all new Tabpages. For examle when I open a new Tabpage the class creates controls and places them.

bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
tp.Controls.Add(bttn1);

So my question is how can I check if this button is clicked?

Also my other question is the same with a Timer tick event.

hichris123
  • 10,145
  • 15
  • 56
  • 70

1 Answers1

1

You can easily attach to the button's Click event from the code:

bttn1.Click += new EventHandler(butt1_Click);

And here's the handler:

void button1_Click(object sender, EventArgs e)
{
    // ...
}

Visual Studio will help you when you type the Click +=. After typing +=, hit the Tab key twice to get the handler.


I hope that you have created a UserControl for this or have sub-classed the TabPage class to create your controls. You should expose the Click event of the button from this newly created class through some new event you create:

public class MyTabPage : TabPage
{
    private Button bttn1;

    public event EventHandler Button1Clicked;

    public MyTabPage()
    {
        bttn1 = new Button();
        bttn1.Name = "button1";
        bttn1.Text = "Start";
        bttn1.Location = new Point(3, 405);
        bttn1.Size = new Size(75, 23);
        bttn1.Click += bttn1_Click;
        this.Controls.Add(bttn1);
    }

    void bttn1_Click(object sender, EventArgs e)
    {
        OnButton1Clicked();
    }

    protected virtual void OnButton1Clicked()
    {
        var h = Button1Clicked;
        if (h != null)
            h(this, EventArgs.Empty);
    }
}

Now when you create an instance of MyTabPage, you can attach a handler to the Button1Clicked event:

MyTabPage page = new MyTabPage();
page.Button1Clicked += page_Button1Clicked;
tabControl.TabPages.Add(page);

...

void page_Button1Clicked(object sender, EventArgs e)
{
}
Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
  • Okay I got it but where to put my button click code? I mean the code that is executed at click – user3741975 Jun 15 '14 at 11:45
  • And I'm getting an error: delegate 'this' can't be NULL. – user3741975 Jun 15 '14 at 12:18
  • Where do you get that error? I do not see anywhere relevant. – Mohammad Dehghan Jun 15 '14 at 12:50
  • page.Button1Clicked += page_Button1Clicked; at this row and i get that at runtime when i want to open a new tabpage – user3741975 Jun 15 '14 at 13:21
  • Tell me the **exact** error message you are getting. I don't understand your error message. There is nothing wrong with that line of code. Did you define the method `page_Button1Clicked` in your class? – Mohammad Dehghan Jun 15 '14 at 13:28
  • Ok the error is gone, but here's my code: http://pastebin.com/hMkw25JS and still when i open new camera the button does nothing. I really appreciate the help – user3741975 Jun 15 '14 at 13:42
  • I made public void tabpage_bttn1Clicked(object sender, EventArgs e) { OnButton1Clicked(); } Tabpage tp = new Tabpage(tabControl1); tp.bttn1Clicked += tp.tabpage_bttn1Clicked; – user3741975 Jun 15 '14 at 14:09
  • Sorry, but you are doing it very wrong! Why is your `Tabpage` class inherits from `Form`?! `Form` class represents a top level window. I am creating a sample project for you. Wait some moments. – Mohammad Dehghan Jun 15 '14 at 14:18
  • Here it is: https://dl.dropboxusercontent.com/u/51708844/SampleTabPageControl.zip – Mohammad Dehghan Jun 15 '14 at 14:57
  • Thank you very much! Last question: can I add parameter to the click event? like void tp_Button2Click(object sender, EventArgs e) this with void tp_Button2Click(object sender, EventArgs e, Tabpage tp) – user3741975 Jun 15 '14 at 15:11
  • Nevermind I got it! Thanks again! Can I somehow upvote you or idk? – user3741975 Jun 15 '14 at 15:12
  • Refer to [this](http://stackoverflow.com/a/3742047/1174942) and [this](http://stackoverflow.com/a/12498589/1174942) answer to understand standard pattern of sending custom arguments to events. To show your appreciation, you can mark my answer as the accepted answer :) Read [here](http://meta.stackexchange.com/a/5235/178969) and [here](http://stackoverflow.com/tour) for more information. – Mohammad Dehghan Jun 15 '14 at 19:10