Launch a button using code

So, I have the following code when the "Add Player" button is clicked

private void addPlayerBtn_Click_1(object sender, EventArgs e) { //Do some code } 

I want to run this code from my SDK. Here is what I tried

 private void command() { addPlayerBtn_Click_1(object sender, EventArgs e); } 

I get a lot of errors as soon as I put in a string

  addPlayerBtn_Click_1(object sender, EventArgs e) 

Can someone please tell me how to write code so that I can trigger an event just by entering it in the code?

+8
source share
6 answers

First, when you call a method, you do not declare the type of the parameter, just the value.

So this is:

 addPlayerBtn_Click_1(object sender, EventArgs e); 

Must be

 addPlayerBtn_Click_1(sender, e); 

Now you need to declare sender and e . These can be actual objects if you have event arguments, or:

 addPlayerBtn_Click_1(null, EventArgs.Empty); 

The above can be used in both WinForms and ASP.NET. In the case of WinForms, you can also call:

 addPlayerBtn.PerformClick(); 
+20
source

When you call a function, you provide the actual arguments, which are the values, not the formal arguments, which are the types and names of the parameters.

Edit

 addPlayerBtn_Click_1(object sender, EventArgs e); 

to

 addPlayerBtn_Click_1(addPlayerBtn, EventArgs.Empty); 
+6
source
 addPlayerBtn_Click_1(null, null); 

This works if you do not need information in sender and e .

+2
source

You are not using a sender or events, so you can call the function directly as follows:

 addPlayerBtn_Click_1(null, null); 
+2
source
 addPlayerBtn_Click_1(object sender, EventArgs e); 

Must be:

 addPlayerBtn_Click_1(this, new EventArgs()); 
+1
source
 private void command() { addPlayerBtn.PerformClick(); } 
0
source

All Articles