0

I have the following: private void ButtonClick(object sender, EventArgs e)

When I add to the parameter list, to make: private void ButtonClick(object sender, EventArgs e, Class c) then it will cause all sorts of problems. However, I need really want in my main:

Class c = new Class();

And then I would like to click a button so that ButtonClick is called, but I really need access to the "Class c" in the function body, so I really need

private void ButtonClick(object sender, EventArgs e, Class c) 

to compile somehow.

I have tried other ways around the issue, such as making Class static, but it would create too much refactoring and cause other errors.

I don't know if this will be possible in Windows Forms. I am not opposed to switching over to XAML and WPF, I just want to know that doing something such as

private void ButtontClick(object sender, EventArgs e, Class c)  

will be possible.

Devin Andres Salemi
  • 2,198
  • 3
  • 12
  • 25
  • From where, you are passing instance of Class c ? – Akash KC Feb 22 '17 at 03:50
  • 2
    Contrary to the title you wrote, this doesn't seem to have anything to do with data-binding. The marked duplicate answers the question text you wrote. In your case, you can subscribe to the `Click` event like this: `button1.Click += (sender, e) => ButtonClick(sender, e, c);`. – Peter Duniho Feb 22 '17 at 04:18

2 Answers2

0

Just make your Class c variable a member variable in your main class where your ButtonClick event handler is located. Unless this ButtonClick event handler is your own custom event handler, you can't add more parameters to the framework's Click event handler for a button. You will have to access your Class c variable as a member variable or use commands and command bindings with command parameters.

user1286901
  • 219
  • 3
  • 10
0

You can put c in the Button.Tag property and use ((Button)sender).Tag in the click event handler to get it.

lantian
  • 34
  • 3