Triggering a button click through code

前端 未结 6 520
暖寄归人
暖寄归人 2021-01-11 15: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         


        
相关标签:
6条回答
  • 2021-01-11 15:37

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

    addPlayerBtn_Click_1(null, null);
    
    0 讨论(0)
  • 2021-01-11 15:40

    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);
    
    0 讨论(0)
  • 2021-01-11 15:44
    addPlayerBtn_Click_1(object sender, EventArgs e);
    

    Should be:

    addPlayerBtn_Click_1(this, new EventArgs());
    
    0 讨论(0)
  • 2021-01-11 15:48
    addPlayerBtn_Click_1(null, null);
    

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

    0 讨论(0)
  • 2021-01-11 16:03

    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();
    
    0 讨论(0)
  • 2021-01-11 16:03
    private void command()
     {     
       addPlayerBtn.PerformClick();
     }
    
    0 讨论(0)
提交回复
热议问题