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
Click a button programmatically
提问by Marc Intes
I want to code a button
that programmatically clicks the other button
when I click it.
我想编写一个当我点击它时button
以编程方式点击另一个的代码button
。
For example, I have two buttons named Button1
and Button2
, what I wanted to do is that immediately after I click Button1
, it should click Button2
. Is this possible?
例如,我有两个名为Button1
and 的按钮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 给你一个有效的。知道您还可以:
raise the
Button2_Click
event usingPerformClick()
method:Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click 'do stuff Me.Button2.PerformClick() End Sub
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
call the
Button2_Click
method using the same arguments thanButton1_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
Button2_Click
使用PerformClick()
方法引发事件:Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click 'do stuff Me.Button2.PerformClick() End Sub
将相同的处理程序附加到许多按钮:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) _ Handles Button1.Click, Button2.Click 'do stuff End Sub
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