0

如何捕获按钮的名称,以便以另一种形式使用它来查询数据库。我是 vb 新手,仍处于早期学习阶段,因此将不胜感激。

主界面

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

框架架

这是我需要捕获名称以查询数据库的地方

4

3 回答 3

3
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
    Dim name As String = DirectCast(sender, Button).Name
End Sub
于 2013-10-25T11:41:48.077 回答
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
于 2013-10-25T11:47:55.780 回答
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
于 2013-10-25T11:23:18.713 回答