如何捕获按钮的名称,以便我可以使用其他形式查询数据库。我是vb的新手,仍然处于早期学习阶段,所以任何帮助都会非常感激。
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
frmRacks
这是我需要捕获名称以查询数据库的地方
答案 0 :(得分:3)
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim name As String = DirectCast(sender, Button).Name
End Sub
答案 1 :(得分:2)
有很多方法可以做到这一点,一个是在frmRack中声明公共字段/变量,另一个是使用ShowDialog而不是Show:
我先与第一个(公共)一起去:
Public buttonName as String
然后在按钮上点击你的frmMain,你传递的值如下:
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.buttonName = "btnA" ' Or you could use DirectCast as proposed by dbasnett
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
然后在加载你的frmRacks时,你现在可以选择分配按钮名称,如:
Private Sub frmRacks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query as String = "SELECT * FROM " + buttonName 'This is only an example you could make your own here
End Sub
答案 2 :(得分:0)
应该是MouseButtons.Left
而不是Windows.Forms.MouseButtons.Left
If e.Button = MouseButtons.Left Then
MsgBox("Left Button Clicked") 'OR WHATEVER YOU WANT IT TO DO?!
End If