How to programmatically trigger an event?

I am creating a C # application for Windows Mobile that needs to programmatically trigger a click event on a button.

I looked at the Button class and see no way to do this.

+3
source share
6 answers

You might want to change your design: at least move the logic with the button1_Click handler somewhere else so that you can call it where you want.

+9
source

You can do something like this:

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

    private void OnButtonClick()
    {

    }

Then you can call in OnButtonClick()where you need to.

+3
source

Control, Button, 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 });
    }

...

+2

, , , :

, , OnClick. OnClick , .

+1

, , onClick?

0

:

someControl_EventOccured(someControl, new EventArgs()); 

, e , .

0

Source: https://habr.com/ru/post/1702477/


All Articles