作为VB的新用户,我很难理解为什么这个代码在一个项目中工作而在另一个项目中不工作。如果我创建一个新项目和2个新表单,此代码可以正常工作,但是当我放入我的项目时,它不会在左键或右键单击时触发。
我尝试过try / catch语句,但没有报告任何错误。如何对此进行故障排除以找出错误。我试图删除代码并在每条评论后运行但仍然相同。我甚至尝试删除表单上的所有其他代码,只留下2个潜艇,但没有快乐。任何帮助将不胜感激。
frmMain
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StorageDataSet1.Customers' table. You can move, or remove it, as needed.
Me.CustomersTableAdapter.Fill(Me.StorageDataSet1.Customers)
'TODO: This line of code loads data into the 'StorageDataSet.User' table. You can move, or remove it, as needed.
Me.UserTableAdapter.Fill(Me.StorageDataSet.User)
'Dim frmDepartmentsLive As New frmDepartment
'frmDepartmentsLive.Owner = Me
'frmDepartmentsLive.ShowDialog()
lblDate.Text = Now
Timer1.Start()
rdoCustomer.Enabled = False
rdoCustomer.Checked = True
rdoDepartment.Enabled = False
rdoDepartment.Checked = False
For Each ctrl In Me.Controls
If TypeOf ctrl Is Button Then
AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
End If
Next
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (e.Button = MouseButtons.Right) Then
Dim btn = CType(sender, Button)
frmRacks.buttonName = btn.Name.Replace("btn", "")
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:1)
由于控件位于面板上,因此它们是该面板控件数组的成员,而不是表单的成员。如果您通过表单的设计器查看(在解决方案资源管理器上,单击“全部显示”,然后打开“{1}}),这一点和其他内容就很明显了。不做任何改变,但它显示了如何创建和添加控件。所以......
formXXX.designer.vb
如果它只是面板上的那些按钮,你可以将其缩短:
For Each ctrl In thepanelName.Controls
If TypeOf ctrl Is Button Then
AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
End If
Next