18

So I have the following code for when the "Add player" button is clicked

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

I want to trigger this code from my SDK however. Here is what I have tried

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

I get lots of errors as soon as I put in the line

 addPlayerBtn_Click_1(object sender, EventArgs e) 

Could somebody please tell me how to write the code so that I can trigger an event by just writting it in code?

0

6 Answers 6

33

For one, when calling a method, you don't declare the type of the parameter, just the value.

So this:

addPlayerBtn_Click_1(object sender, EventArgs e);

Should be

addPlayerBtn_Click_1(sender, e);

Now, you'll have to declare sender and e. These can be actual objects, if you have event args to pass, or:

addPlayerBtn_Click_1(null, EventArgs.Empty);

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

addPlayerBtn.PerformClick();
7

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

Change

addPlayerBtn_Click_1(object sender, EventArgs e);

to

addPlayerBtn_Click_1(addPlayerBtn, EventArgs.Empty);
3
private void command()
 {     
   addPlayerBtn.PerformClick();
 }
2
addPlayerBtn_Click_1(null, null);

This works if you don't need the information in sender and e.

2

You are not using the sender or the events so you can call the function directly like this:

addPlayerBtn_Click_1(null, null);
1
addPlayerBtn_Click_1(object sender, EventArgs e);

Should be:

addPlayerBtn_Click_1(this, new EventArgs());
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.