如何以编程方式调用事件?

时间:2022-08-26 00:06:51

I am creating a C# Windows Mobile application that I need to programmatically invoke the click event on a Button.

我正在创建一个C#Windows Mobile应用程序,我需要以编程方式调用Button上的click事件。

I have looked at the Button class and do not see a way to do this.

我查看了Button类,但没有看到这样做的方法。

6 个解决方案

#1


You might consider changing your design: at least move the logic from button1_Click handler somewhere else so that you'll be able to invoke it from wherever you want.

您可以考虑更改您的设计:至少将逻辑从button1_Click处理程序移动到其他位置,以便您可以从任何您想要的地方调用它。

#2


You can do something like this:

你可以这样做:

    private void button1_Click(object sender, EventArgs e)
    {
        OnButtonClick();
    }

    private void OnButtonClick()
    {

    }

Then you can call your OnButtonClick() wherever you need to.

然后,您可以在任何需要的地方调用OnButtonClick()。

#3


The Control class, from which Button inherits, has the OnClick method, which invokes the Click event. Using reflection, you can call this method, even though it is declared protected. For instance, I created the following extension method:

Button继承的Control类具有OnClick方法,该方法调用Click事件。使用反射,即使声明受保护,也可以调用此方法。例如,我创建了以下扩展方法:

public static void PerformClick(this Control value)
    {
        if (value == null)
            throw new ArgumentNullException();

        var methodInfo = value.GetType().GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
        methodInfo.Invoke(value, new object[] { EventArgs.Empty });
    }

Although, this may be regarded as a dirty hack...

虽然,这可能被视为一个肮脏的黑客......

#4


Not exactly the answer you were probably looking for:::

不完全是您可能正在寻找的答案:::

You might just call the code you want to run, move the meat of the code outside the OnClick handling method. The OnClick method could fire that method the same as your code.

您可能只需调用要运行的代码,在OnClick处理方法之外移动代码。 OnClick方法可以像您的代码一样触发该方法。

#5


Anything that prevents you from simply invoking the method that handles the onClick event?

什么阻止你只是调用处理onClick事件的方法?

#6


You can call the added event handler function as:

您可以将添加的事件处理函数调用为:

someControl_EventOccured(someControl, new EventArgs()); 

This works when the control's event argument e is not used inside the handler function, and mostly it's not used.

当控制器的事件参数e未在处理函数内部使用时,这种方法很有效,并且大部分都没有使用它。

#1


You might consider changing your design: at least move the logic from button1_Click handler somewhere else so that you'll be able to invoke it from wherever you want.

您可以考虑更改您的设计:至少将逻辑从button1_Click处理程序移动到其他位置,以便您可以从任何您想要的地方调用它。

#2


You can do something like this:

你可以这样做:

    private void button1_Click(object sender, EventArgs e)
    {
        OnButtonClick();
    }

    private void OnButtonClick()
    {

    }

Then you can call your OnButtonClick() wherever you need to.

然后,您可以在任何需要的地方调用OnButtonClick()。

#3


The Control class, from which Button inherits, has the OnClick method, which invokes the Click event. Using reflection, you can call this method, even though it is declared protected. For instance, I created the following extension method:

Button继承的Control类具有OnClick方法,该方法调用Click事件。使用反射,即使声明受保护,也可以调用此方法。例如,我创建了以下扩展方法:

public static void PerformClick(this Control value)
    {
        if (value == null)
            throw new ArgumentNullException();

        var methodInfo = value.GetType().GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
        methodInfo.Invoke(value, new object[] { EventArgs.Empty });
    }

Although, this may be regarded as a dirty hack...

虽然,这可能被视为一个肮脏的黑客......

#4


Not exactly the answer you were probably looking for:::

不完全是您可能正在寻找的答案:::

You might just call the code you want to run, move the meat of the code outside the OnClick handling method. The OnClick method could fire that method the same as your code.

您可能只需调用要运行的代码,在OnClick处理方法之外移动代码。 OnClick方法可以像您的代码一样触发该方法。

#5


Anything that prevents you from simply invoking the method that handles the onClick event?

什么阻止你只是调用处理onClick事件的方法?

#6


You can call the added event handler function as:

您可以将添加的事件处理函数调用为:

someControl_EventOccured(someControl, new EventArgs()); 

This works when the control's event argument e is not used inside the handler function, and mostly it's not used.

当控制器的事件参数e未在处理函数内部使用时,这种方法很有效,并且大部分都没有使用它。