我正在尝试创建允许我重置按钮的子项。我将有很多按钮,并且想在按钮中调用一个sub来使代码更整洁。有人可以指导我如何执行此操作
Private Sub Button2_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown, Button1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
btn = DirectCast(sender, Button)
ButtonReset()
End If
End Sub
Public Sub ButtonReset()
Select Case MsgBox("Do you want to reset?", MsgBoxStyle.YesNoCancel, "caption")
Case MsgBoxResult.Yes
btn2_Count = 0
MyInterval = 1
Button2.Text = "Station 2"
Button2.BackColor = DefaultBackColor
Case MsgBoxResult.Cancel
Exit Sub
Case MsgBoxResult.No
Exit Sub
End Select
End Sub
答案 0 :(得分:0)
如上所述,@ jmcilhinney无需使用Select Case
。
我在这里写一个小例子,可能会有所帮助。但是,我不会包含
btn2_Count = 0
MyInterval = 1
因为我不是他们的100%。
还有另一件事脱颖而出。您似乎将btn作为按钮(两个按钮之一),然后调用了一个硬编码为button2的子。似乎打败了整个观点。
根据我的回答,我假设您要的是按钮1的“ Station 1”和按钮2的“ Station 2”? 您可以将数字预加载到按钮的'.Tag'属性中,并使用该数字来确定要处理哪个按钮。
例如,类似:
Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown, Button2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
If MessageBox.Show("Do you want to reset?", "Caption", MessageBoxButtons.OKCancel) = DialogResult.OK Then
Dim btn As Button = DirectCast(sender, Button)
btn.Text = String.Format("Station {0}", btn.Tag.ToString)
btn.BackColor = DefaultBackColor
End If
End If
End Sub