How do I set a click event for a form?

前端 未结 6 724
名媛妹妹
名媛妹妹 2021-01-05 10:37

I have a c# form (let\'s call it MainForm) with a number of custom controls on it. I\'d like to have the MainForm.OnClick() method fire anytime someone clicks on the form re

6条回答
  •  一生所求
    2021-01-05 11:13

    In the form's ControlAdded event, add a MouseClick handler to the control, with the Address of the form's click event. I haven't tested this, but it might work.

    Private Sub Example_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
    
        AddHandler e.Control.MouseClick, AddressOf Example_MouseClick
    End Sub
    
    Private Sub Example_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        MessageBox.Show("Click")
    End Sub
    

提交回复
热议问题