vb.net 以编程方式单击按钮

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

Click a button programmatically

vb.netwinformsbutton

提问by Marc Intes

I want to code a buttonthat programmatically clicks the other buttonwhen I click it.

我想编写一个当我点击它时button以编程方式点击另一个的代码button

For example, I have two buttons named Button1and Button2, what I wanted to do is that immediately after I click Button1, it should click Button2. Is this possible?

例如,我有两个名为Button1and 的按钮Button2,我想做的是在我单击后立即Button1单击Button2。这可能吗?

回答by Chris

Best implementation depends of what you are attempting to do exactly. Nadeem_MK gives you a valid one. Know you can also:

最佳实施取决于您正试图做什么。Nadeem_MK 给你一个有效的。知道您还可以:

  1. raise the Button2_Clickevent using PerformClick()method:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'do stuff
        Me.Button2.PerformClick()
    End Sub
    
  2. attach the same handler to many buttons:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) _
        Handles Button1.Click, Button2.Click
        'do stuff
    End Sub
    
  3. call the Button2_Clickmethod using the same arguments than Button1_Click(...)method (IFyou need to know which is the sender, for example) :

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'do stuff
         Button2_Click(sender, e)
    End Sub
    
  1. Button2_Click使用PerformClick()方法引发事件:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'do stuff
        Me.Button2.PerformClick()
    End Sub
    
  2. 将相同的处理程序附加到许多按钮:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) _
        Handles Button1.Click, Button2.Click
        'do stuff
    End Sub
    
  3. Button2_Click使用与方法相同的参数调用Button1_Click(...)方法(例如,如果您需要知道哪个是发件人):

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'do stuff
         Button2_Click(sender, e)
    End Sub
    

回答by sk2185

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button2_Click(Sender, e)
End Sub

This Code call button click event programmatically

此代码以编程方式调用按钮单击事件

回答by Nadeem_MK

The best practice for this sort of situation is to create a method that hold all the logics, and call the method in both events, rather than calling an event from another event;

这种情况的最佳实践是创建一个包含所有逻辑的方法,并在两个事件中调用该方法,而不是从另一个事件中调用一个事件;

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        LogicMethod()

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        LogicMethod()

End Sub

Private Sub LogicMethod()

     // All your logic goes here

End Sub

In case you need the properties of the EventArgs (e), you can easily pass it through parameters in your method, that will avoid errors if ever the sender is of different types. But that won't be a problem in your case, as both senders are of type Button.

如果您需要 EventArgs (e) 的属性,您可以轻松地通过方法中的参数传递它,如果发送者是不同类型的,这将避免错误。但这在您的情况下不会成为问题,因为两个发件人都是 Button 类型。

回答by Pradip

Let say button 1 has an event called

假设按钮 1 有一个名为

Button1_Click(Sender, eventarg)

If you want to call it in Button2 then call this function directly.

如果要在 Button2 中调用它,则直接调用此函数。

Button1_Click(Nothing, Nothing)

回答by u2841295

in c# this is working :D

在 C# 中这是有效的:D

protect void button1_Click(object sender, EventArgs e){
    button2_Click(button2, null);
}

protect void button2_Click(object sender, EventeArgs e){
    //some codes here
}

for vb.net

对于 vb.net

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)   Handles Button1.Click
    Button2_Click(Sender, e)
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)   Handles Button2.Click
    //some codes here
End Sub