0

I've seen many examples of how to add a click event to a dynamically created button, but none of the examples show how to pass arguments with the event.

I am new to C# and just looking for a simple solution. See the code below:

public partial class Main : Form {
    public Main() {
        InitializeComponent();

        // Add a button dynamically
        Button Test = new Button();
        this.Controls.Add(Test);
        Test.Left = 0;
        Test.Top = 0;
        Test.Width = 100;
        Test.Height = 20;
        Test.Text = "Hello";
        int param1 = 1;
        string param2 = "Test";

        // Add click event handler with parameters ????
        // I know this syntax is wrong, but how do I get param1 & 2
        // into the Test_Click ????
        Test.Click += Test_Click(param1,param2);
    }

    private void Test_Click(int param1, string param2) {
        MessageBox.Show(param1.ToString() + ": " + param2);
    }
 
6
  • The event passes parameter arguments to the event handler, not your code. So, how many and which types of parameter an event handler must have is entirely specified by the event. Why exactly do you want to pass some values from the Main constructor to the event handler to begin with? Why can't the event handler not itself possess or aquire these values without the help of the Main constructor? What precisely makes you think/decide that the Main constructor has to deal with some event handler values? Commented Sep 4, 2022 at 18:13
  • There are of course ways. Like for example wrapping your Test_Click method in an anonymous function using a lambda expression, with the anonymous function basically being the real event handler that is being subscribed to the Test.Click event. In the anonymous function, you would then be able to call Test_Click with whatever parameters you like. Although, as said, i don't quite get why the Main constructor must/should be involved in providing values to the click handler to begin with... Commented Sep 4, 2022 at 18:16
  • @MySkullCaveIsADarkPlace As I said, I am a new to C#, so talking in terms like "using a lambda expression" and an "anonymous function" have no meaning to me. Please post a possible solution with code example instead of confusing me more. Also, please do not ask why I am trying to do what I am doing. This is just a simplified example for what I am really trying to accomplish. I just provided a very small code snippet to isolate my question. Commented Sep 4, 2022 at 19:24
  • Your click handler will receive object sender, EventArgs e as arguments no matter how much you wish something different. What you can do is create a Dictionary, indexed by, say, the Button's Name property and with a value of the Tuple of the arguments you want to pass. In the handler, test the sender for its button-ness, cast it to a Button, extract the Name and use it to look up you parameters in the Dictionary Commented Sep 4, 2022 at 19:51
  • @Flydog57 As I said previously, I am new to C#, so instead of throwing terminology at me like "Dictionary" and "Tuple", can you please reply to the post as an Answer (not a comment) and provide code example. I think I understand what you are saying and think it may work, basically storing the parameters elsewhere, and having the function retrieve the parameters based on the button name. Commented Sep 4, 2022 at 20:00

4 Answers 4

1

You do not provide arguments when adding the event but you provide it in the event it self, in this case the click event arguments are:

private void Test_Click(object sender, EventArgs e) 
{
    
}

the first argument is usually object sender while the second changes depending on the event type, in case of click event it's "EventArgs e"

and for the adding event :

Test.Click += Test_Click;

Hope i helped you.

Sign up to request clarification or add additional context in comments.

4 Comments

No, it does not help. It does not answer the question. Syntactically you are correct, but then how do I pass the int and the string ? I am looking to have a click event, but pass it the parameters specified so I can make multiple buttons call the same function with different parameters.
Do you mean linking the same event to multiply buttons and for example doing an action about the pressed button? I believe you can't use an button event as a method but you can do it this way
@Omaar I want to have multiple buttons all call the same function with different parameters. However, I want to create the buttons dynamically. The buttons will then be specified in an external location with the necessary parameters and read in during runtime. If you have a way of doing that, please provide code example.
@Omaar Specifically, how do I get param1 and param2 into the Test_Click ?
0

Ok. I know this is a little ugly, but this is what I came up with from the comments above. If there is a better way to do this, please let me know:

public partial class Main : Form {
    public Dictionary<object, Tuple<int, string>> Params = new Dictionary<object, Tuple<int,string>>();
    public Main() {
        InitializeComponent();

        // Add a button dynamically
        Button Test = new Button();
        this.Controls.Add(Test);
        Test.Left = 0;
        Test.Top = 0;
        Test.Width = 100;
        Test.Height = 20;
        Test.Text = "Hello";
        Test.Name = "Test";
        
        // Add click event handler with parameters
        Params.Add(Test, new Tuple<int, string>(1, "Test"));
        Test.Click += Test_Click;
    }

    private void Test_Click(object sender, EventArgs e) {
        Tuple<int,string> value;
         if (Params.TryGetValue(sender, out value)) {
            MessageBox.Show(value.Item1.ToString() + ": " + value.Item2);
        }
        else {
            MessageBox.Show(sender.ToString() + " not found.");
        }
    }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Here is another solution I came up with without using Dictionary or Tuple by adding the button and the parameters together into a class structure. I like this one better:

class MyButton {
    private Button oButton;
    private int iParam1;
    private string sParam2;
    public MyButton(Form Me, int Left, int Top, int Width, int Height, string Text, int Param1, string Param2) {
        oButton = new Button();
        Me.Controls.Add(oButton);
        oButton.Left = Left;
        oButton.Top = Top;
        oButton.Width = Width;
        oButton.Height = Height;
        oButton.Text = Text;
        iParam1 = Param1;
        sParam2 = Param2;
        oButton.Click += Click;
    }
    private void Click(object sender, EventArgs e) {
        MessageBox.Show(iParam1.ToString() + ": " + sParam2);
    }
}
public partial class Main : Form {
    public Main() {
        InitializeComponent();
        MyButton Test = new MyButton(this, 0, 0, 100, 20, "Hello", 1, "Test");
    }
}

Comments

0

Here's a bit cleaner version of what I was talking about (sorry, I wasn't near a computer earlier). The initial implementation of Tuple was truly clunky; it's since become part of the language.

I started with the auto-generated:

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
    }
}

And then added this to the form class:

private Dictionary<string, (int param1, string param2)> _parametersMap = new Dictionary<string, (int param1, string param2)>();

If you are using the latest compiler, you can simplify this to:

private Dictionary<string, (int param1, string param2)> _parametersMap = new();

Then I added a method to the form class that the click handler will call:

public void ShowSomething (string buttonName, int i, string s)
{
    MessageBox.Show(this, $"Button: {buttonName} cliked: i={i}, s = {s}");
}

All button click handlers have the same method signature. It's determined by the code that raises the click event. So...

public void OnDynamicButtonClick (object sender, EventArgs e)
{
    if (sender is Button button)
    {
        var (param1, param2) = _parametersMap[button.Name];
        ShowSomething(button.Name, param1, param2);
    }
}

Notice that the if statement uses a pattern matching if mechanism. The code within the if sees button as the sender cast to a Button.

Then, to match your initial code, I put this in the form constructor (after InitializeComponent. It really doesn't belong there. It should be in some event handler, at the very least the Form Load handler, more likely wherever it is that you want to create the buttons (creating them in the constructor kind of defeats the idea of dynamically constructing them):

var firstButton = new Button
{
    Name = "FirstButton",
    Text = "First",
    Location = new Point(100, 100),
    Size = new Size(200, 50),
    Parent = this,
};
firstButton.Click += OnDynamicButtonClick;
_parametersMap.Add(firstButton.Name, (42, "Test1"));
Controls.Add(firstButton);

var secondButton = new Button
{
    Name = "SecondButton",
    Text = "Second",
    Location = new Point(100, 300),
    Size = new Size(200, 50),
    Parent = this,
};
secondButton.Click += OnDynamicButtonClick;
_parametersMap.Add(secondButton.Name, (111, "Test2"));
Controls.Add(firstButton);

Note that I created two buttons, both pointing to the same button click handler (that's the point of the sender argument.

If you are going to be doing this a lot, an alternative approach would be to sub-class the Button class, adding your Param1 and Param2 as properties:

public class MyButtonSubClass : Button
{
    public int Param1 { get; set; }
    public string Param2 { get; set; }
}

(hopefully giving them more meaningful names)

Instances of your subclass are Buttons, just with two extra properties, so you can do this:

 var firstButton = new MyButtonSubclass
 {
     Name = "FirstButton",
     Text = "First",
     Location = new Point(100, 100),
     Size = new Size(200, 50),
     Parent = this,
     Param1 = 42,
     Param2 = "Some Test",
 };
 firstButton.Click += OnDynamicButtonClick;
 Controls.Add(firstButton);
 //same for the second button

And then change the click event handler to:

public void OnDynamicButtonClick (object sender, EventArgs e)
{
    if (sender is MyButtonSubclass button)
    {
        ShowSomething(button.Name, button.Param1, button.Param2);
    }
}

And the program will appear to work in the same way.

Or, at this point, you could change the event handler to look like:

public void OnDynamicButtonClick(object sender, EventArgs e)
{
    if (sender is MyButtonSubclass button)
    {
        MessageBox.Show(this, $"Button: {button.Name} cliked: i={button.Param1}, s = {button.Param1}");
    }
}

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.