2013-10-27 124 views
2

作爲VB的新用戶,我很努力地想知道爲什麼這個代碼可以在一個項目中工作,而不是在另一個項目中工作。如果我創建一個新項目和2個新窗體,但是當我放置在我的項目中時,此代碼工作正常,但無論是左鍵還是右鍵單擊都不會觸發。.Net WindowsForms MouseDown事件沒有觸發

我試過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

是控制另一個控制或某事的孩子? –

+0

@ta no。這只是一個按鈕ctrl。謝謝 – user1532468

+0

開始設置一個斷點上如果(e.Button = MouseButtons.Right)然後'看看它是否有火災,然後一步一步看看有什麼問題 – Plutonix

回答

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 

如果它僅僅是面板,您可以剪短它放在這些按鈕:

For Each btn As Button In thepanelName.Controls 
     AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown 
    Next