C# 以编程方式触发控件的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12184614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:23:28  来源:igfitidea点击:

Trigger control's event programmatically

c#winformseventscontrols

提问by steavy

Assume that I have a WinFoms project. There is just one button (e.g. button1).

假设我有一个 WinFoms 项目。只有一个按钮(例如button1)。

The question is: is it possible to trigger the ButtonClickedevent via code without really clicking it?

问题是:是否可以ButtonClicked通过代码触发事件而无需真正点击它?

采纳答案by itsme86

Button controls have a PerformClick() method that you can call.

按钮控件具有可以调用的 PerformClick() 方法。

button1.PerformClick();

回答by C???

You can just call the event handler function directly and specify nullfor the sender and EventArgs.Emptyfor the arguments.

您可以直接调用事件处理函数并指定null发送者和EventArgs.Empty参数。

void ButtonClicked(object sender, EventArgs e)
{
    // do stuff
}

// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);

// call the event handler directly:
ButtonClicked(button1, EventArgs.Empty);

Or, rather, you'd move the logic out of the ButtonClickedevent into its own function, and then your event handler and the other code you have would in turn call the new function.

或者,更确切地说,您将逻辑从ButtonClicked事件移到其自己的函数中,然后您的事件处理程序和您拥有的其他代码将依次调用新函数。

void StuffThatHappensOnButtonClick()
{
    // do stuff
}

void ButtonClicked(object sender, EventArgs e)
{
    StuffThatHappensOnButtonClick();
}

// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);

// Simulate the button click:
StuffThatHappensOnButtonClick();

The latter method has the advantage of letting you separate your business and UI logic. You really should never have any business logic in your control event handlers.

后一种方法的优点是让您将业务和 UI 逻辑分开。你真的不应该在你的控制事件处理程序中有任何业务逻辑。

回答by coolmine

button1.PerformClick();

But if you have to do something like this maybe it's better to move the code you have under the event on a new method ?

但是,如果您必须做这样的事情,也许最好将事件下的代码移动到新方法上?

回答by Tarzan

Yes, just call the method the way you would call any other. For example:

是的,只需像调用其他方法一样调用该方法即可。例如:

    private void btnSayHello_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello World!");
    }

    private void btnTriggerHello_Click(object sender, EventArgs e)
    {
        btnSayHello_Click(null, null);
    }

回答by TomBot

The .NET framework uses a pattern where for every event Xthere is a method protected void OnX(EventArgs e) {}that raises event X. See this Msdn article. To raise an event from outside the declaring class you will have to derive the class and add a public wrapper method. In the case of Buttonit would look like this:

.NET 框架使用一种模式,其中对于每个事件X都有一个protected void OnX(EventArgs e) {}引发 event的方法X。请参阅这篇MSDN 文章。要从声明类外部引发事件,您必须派生该类并添加一个公共包装器方法。在这种情况下,Button它看起来像这样:

class MyButton : System.Windows.Forms.Button
{

    public void ProgrammaticClick(EventArgs e)
    {
        base.OnClick(e);
    }

}

回答by user3765450

use a for loop to call the button_click event

使用 for 循环调用 button_click 事件

private void btnadd_Click(object sender, RoutedEventArgs e)
{ 
    for (int i = 0; i <= 2; i++)
        StuffThatHappensOnButtonClick(); 
}


void StuffThatHappensOnButtonClick()
{
    ........do stuff
}

we assume at least one time you need click the button

我们假设您至少需要单击一次按钮

回答by IamBatman

Why don't you just put your event code into a Method. Then have the Event execute the method. This way if you need to execute the same code that the Event rises, you can, but simply just calling the "Method".

你为什么不把你的事件代码放到一个方法中。然后让事件执行该方法。这样,如果您需要执行与事件上升相同的代码,则可以,但只需调用“方法”即可。

void Event_Method()
{
    //Put Event code here.
    MessageBox.Show("Hello!");
}

void _btnSend_Click(object sender, EventArgs e)
{
    Event_Method();
}

void AnotherMethod()
{
    Event_Method();
}

Make sense? Now the "Click" event AND anywhere in code you can trigger the same code as the "Click" event.

有道理?现在“点击”事件和代码中的任何地方都可以触发与“点击”事件相同的代码。

Don't trigger the event, call the method that the event calls. ;)

不触发事件,调用事件调用的方法。;)

回答by Jonathan

In most cases you would not need to do that. Simply wrap your functionality in functions related to a specific purpose (task). You call this function inside your event and anywhere else it's needed.

在大多数情况下,您不需要这样做。只需将您的功能包装在与特定目的(任务)相关的函数中。您可以在您的事件中以及其他任何需要它的地方调用此函数。

Overthink your approach.

过度思考你的方法。