Home > Enterprise >  When generating multiple buttons in C# how do I give each one a separate function?
When generating multiple buttons in C# how do I give each one a separate function?

Time:02-09

In my code, I generate one extra label and button every time a different button is clicked. I want it so each button can be programmed with a different function. Ex: If I clicked on generated button1 I would be brought to page 1. If I clicked on generated button2 I would be brought to page 2 etc. What would be the best way to approach this?

Here is my code below:

if (task1 == false)
            {
                Label taskLabel = new Label();
                taskLabel.Text = taskInput.Text;
                taskLabel.Location = new Point(63, 183);
                taskLabel.BorderStyle = BorderStyle.FixedSingle;
                taskLabel.AutoSize = true;
                page2.Controls.Add(taskLabel);
                labelHeight = taskLabel.Bottom;
                labelWidth = taskLabel.Right;

                
                Button taskButton  = new Button();
                taskButton.Text = "Let's Go";
                taskButton.Location = new Point(labelWidth   30, 180);
                taskButton.AutoSize = true;
                page2.Controls.Add(taskButton);
                taskInput.Text = "";
                task1 = true;

            }
            else
            {
                Label taskLabel = new Label();
                taskLabel.Text = taskInput.Text;
                taskLabel.Location = new Point(63, labelHeight   30);
                taskLabel.BorderStyle = BorderStyle.FixedSingle;
                taskLabel.AutoSize = true;
                page2.Controls.Add(taskLabel);

                Button taskButton = new Button();
                taskButton.Text = "Let's Go";
                labelHeight = taskLabel.Bottom;
                labelWidth = taskLabel.Right;
                taskButton.Location = new Point(labelWidth   30, labelHeight - 18);
                taskButton.AutoSize = true;
                buttonHeight = taskButton.Top;
                page2.Controls.Add(taskButton);
                taskInput.Text = "";

CodePudding user response:

After you create the button you add a handler to the function that you want it to run.

Button taskButton  = new Button();
taskButton.Click  = FunctionYouWantToRun();

CodePudding user response:

You can set the click handler for each button you create like this

Button taskButton1 = new Button;
taskButton1.Click  = taskButton1_Click;

private void taskButton1_Click(object sender, EventArgs e)
{
    // code for the first button
}


Button taskButton2 = new Button;
taskButton2.Click  = taskButton2_Click;

private void taskButton2_Click(object sender, EventArgs e)
{
    // code for the second button
}


If you have many buttons you could assign them all the same click event and in that click event determine which button was clicked

Button taskButton1 = new Button;
taskButton1.Tag = 1;
taskButton1.Click  = taskButton_Click;

Button taskButton2 = new Button;
taskButton2.Tag = 2;
taskButton2.Click  = taskButton_Click;

Button taskButton3 = new Button;
taskButton3.Tag = 3;
taskButton3.Click  = taskButton_Click;


private void taskButton_Click(object sender, EventArgs e)
{
    if ( ((Button)sender).Tag == 1) then
    {
         // code for the first button
    }
    else if ( ((Button)sender).Tag == 2) then
    {
         // code for the second button
    }
    else if ( ((Button)sender).Tag == 3) then
    {
         // code for the third button
    }

}

or without using the Tag property

Button taskButton1 = new Button;
taskButton1.Click  = taskButton_Click;

Button taskButton2 = new Button;
taskButton2.Click  = taskButton_Click;

Button taskButton3 = new Button;
taskButton3.Click  = taskButton_Click;


private void taskButton_Click(object sender, EventArgs e)
{
    if (sender == Button1) then
    {
         // code for the first button
    }
    else if (sender == Button2) then
    {
         // code for the second button
    }
    else if (sender == Button3) then
    {
         // code for the third button
    }

}
  •  Tags:  
  • Related