Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering a button click through code

Tags:

c#

events

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?

like image 331
Ralt Avatar asked Dec 24 '12 19:12

Ralt


People also ask

How do you trigger button click on Enter in HTML?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you call a button click event in code behind?

Button event is always trigger when you click the button but if you want to raise your button click event manually from code then you can use Button. PerformClick() from your code. Suppose on click of your Button1 you want to trigger Button2 click event then Please refer this[^] MSDN example.

How do I invoke button click event in unity?

gameObject. GetComponent<Button>(). Click();


2 Answers

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();
like image 148
Dave Zych Avatar answered Oct 12 '22 23:10

Dave Zych


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);
like image 44
Ben Voigt Avatar answered Oct 13 '22 00:10

Ben Voigt