简体   繁体   English

如何手动执行button_click?

[英]How to execute button_click manually?

On a C#-Form I have a button. 在C#表单上,我有一个按钮。 When I double-click the button then it automatically creates the function 当我双击按钮时,它将自动创建函数

 private void button_Click(object sender, EventArgs e).

This works fine. 这很好。

My question is: How can I manually execute this function? 我的问题是:如何手动执行此功能? I want to run in once in the constructor? 我想在构造函数中一次运行吗?

My question is: How can i manually execute this function? 我的问题是:如何手动执行此功能?

Do not execute button click manually. 不要手动执行按钮单击。 It's purpose to be executed only when button raises click event. 目的是仅在按钮引发click事件时才执行。

I want to run in once in the constructor? 我想在构造函数中一次运行吗?

Extract code which you have in this event handler to separate method and run it both from c constructor and handler: 提取此事件处理程序中具有的代码以分离方法,然后从c构造函数和处理程序中运行它:

public Form1()
{
    InitializeComponent();
    DoSomething();
}

private void button_Click(object sender, EventArgs e)
{
    DoSomething();
}

private void DoSomething()
{
    // extract handler code here
}

You can try this: 您可以尝试以下方法:

button_Click(button,new EventArgs());

or you can do 或者你可以做

button.PerformClick();
button.PerformClick();

参见MSDN

Its better if you transfer the code from button_click to new function, and call that one. 如果将代码从button_click转移到新函数,然后调用该函数,效果会更好。 Like this: 像这样:



    void MyFunc()
    {
        //code that was previously in button_click
    }
    private void button_Click(object sender, EventArgs e)
    {
        MyFunc();
    }

    void OtherFunction()
    {
        MyFunc();
    }

That function is an event handler: a function that gets executed as a response to an event, in this case a user event: the user clicked a button. 该函数是一个事件处理程序:作为对事件的响应而执行的函数,在本例中为用户事件:用户单击了按钮。

If you need to execute the very same action without being triggered with an event, the correct approach is move the code that performs the action to a separate method 如果您需要执行完全相同的动作而不会被事件触发,则正确的方法是将执行该动作的代码移到单独的方法

private void ExecuteMyAction()
{
   // Do stuff
}

private void button_Click(object sender, EventArgs e)
{ 
   ExecuteMyAction()
}

And then you can call safely that method in your constructor 然后您可以在构造函数中安全地调用该方法

public class MyClass
{ 
   public MyClass()
   {
      ExecuteMyAction();
   }
}

Actually it is a better to keep the code in a handler as lean as possible and move the code that performs that action to another method. 实际上,最好将代码尽可能地保持在处理程序中,并将执行该操作的代码移到另一个方法。 In the handler you just need to gather the information needed to perform an action that it is only available when the event is processed and then pass that information to another part of the system that is in charge of processing that information. 在处理程序中,您只需要收集执行仅在处理事件时可用的操作所需的信息,然后将该信息传递给负责处理该信息的系统的另一部分。

For example, if you need the user to input a name in a Textbox when the user clicks a button, the button_click handler should only get the string from the text box and call a method that saves that information, passing the string as a parameter. 例如,如果您需要用户在单击按钮时在文本框中输入名称,则button_click处理程序应仅从文本框中获取字符串,然后调用保存该信息的方法,并将字符串作为参数传递。

That way you reduce coupling by keeping separated the code that performs the action (business code) from the code that commands the action to be performed (which is boilerplate code that depends on the framework you use) 这样,通过将执行动作的代码(业务代码)与命令执行动作的代码分开(这是取决于您使用的框架的样板代码),可以减少耦合

您可以这样称呼它:

button_Click(this, EventArgs.Empty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM