C# 如何从另一种方法调用按钮单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11518529/
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 18:09:27  来源:igfitidea点击:

How to call a button click event from another method

c#

提问by User1979

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args)from another method?

如何SubGraphButton_Click(object sender, RoutedEventArgs args)从其他方法调用?

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   // call SubGraphButton-Click().
}

采纳答案by patel.milanb

You can call the button_click event by simply passing the arguments to it:

您可以通过简单地将参数传递给它来调用 button_click 事件:

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click(sender, args);
}

回答by zmbq

You can simply call it:

你可以简单地调用它:

SubGraphButton_Click(sender, args);

Now, if your SubGraphButton_Clickdoes something with the args, you might be in trouble, but usually you don't do anything with them.

现在,如果你SubGraphButton_Click对 args 做了一些事情,你可能会遇到麻烦,但通常你不会对它们做任何事情。

回答by Germann Arlington

Usually the better way is to trigger an event (click) instead of calling the method directly.

通常更好的方法是触发一个事件(点击)而不是直接调用方法。

回答by m.edmondson

Add it to the instance of the Click delegate:

将其添加到 Click 委托的实例中:

ChildNode.Click += SubGraphButton_Click

ChildNode.Click += SubGraphButton_Click

which is inkeeping with the pattern .NET events follow (Observer).

这与 .NET 事件遵循的模式(观察者)保持一致。

回答by Pedram

You can easily do it by the following piece of code (assuming that name of your button is btnButton):

您可以通过以下代码轻松完成(假设您的按钮名称是btnButton):

btnButton.PerformClick();

回答by Nima Soroush

You can perform different approaches to work around this. The best approach is, if your both buttons are suppose to do the same job, you can define a third function to do the job. for example :

您可以执行不同的方法来解决此问题。最好的方法是,如果你的两个按钮都假设做同样的工作,你可以定义第三个功能来完成这项工作。例如 :

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
    myJob()  
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
    myJob()
}

private void myJob()
{
    // Your code here
}

but if you are still persisting on doing it in your way, the best action is :

但如果你仍然坚持按照自己的方式去做,最好的做法是:

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click.PerformClick();
}

回答by Dark Knight

you can call the button_click event by passing..

您可以通过传递来调用 button_click 事件..

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click(sender, args);
}

Also without passing..

也没有通过..

private void SubGraphButton_Click(object sender, EventArgs args)
{
}

private void Some_Method() //this method is called
{
   SubGraphButton_Click(new object(), new EventArgs());
}

回答by Aphonk Rofest

private void PictureBox1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Click Succes");
}

private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)
    {
        PictureBox1_Click(sender, e); //or try this one "this.PictureBox1_Click(sender, AcceptButton);"
    }
}

回答by user6648617

For me this worked in WPF

对我来说,这在 WPF 中有效

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent, Button_OK);
            Button_OK.RaiseEvent(routedEventArgs);
        }
    }

回答by ExcelinEfendisi

Use InvokeOnClick event. it works even if the button is invisible/disabled

使用 InvokeOnClick 事件。即使按钮不可见/禁用,它也能工作