How can I automatically handle form events in another class / module in VB.NET?

Here is my code:

Public Class Form1

End Class

Public Class Form1Handler
    Inherits Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("I")
    End Sub
End Class

I am trying to get Form1Handler to automatically process Form1 events. How can i do this? Should I use a module? I am doing this in Visual Basic 2010.

I do not need to create an event handler in Form1 and then pass it to another class / module. Is there a way to automatically "broadcast" events from form1 to form1handler?

+3
source share
4 answers

, , . , mbutton:

Public Class mb
  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mButton.Click
  MsgBox("click")
  End Sub
End Class

:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  b = New mb
  Me.Controls.Add(b)
  b.Name = "testbutton"
  b.Left = 40 : b.Top = 40 : b.Width = 90 : b.Height = 50
End Sub

click mb.

+3

, , . , - . xpda , , , xpda.

Form1 2 (Buttton1 Button2) (textbox1). (Module1). , .

Module Module1
Public WithEvents ButtonClick1 As Button = Form1.Button1
Public WithEvents ButtonClick2 As Button = Form1.Button2

Public Sub MyButtons(sender As System.Object, e As System.EventArgs) Handles _
ButtonClick1.Click, 
ButtonClick2.Click

    If sender Is ButtonClick1 Then Form1.TextBox1.Text = "Button1 clicked"
    If sender Is ButtonClick2 Then Form1.TextBox1.Text = "Button2 clicked"
End Sub
End Module

WithEvents, , . , WithEvents ( ) Handles Sub MyButtons.

Form Load . . , .

1 :

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Module1.MyButtons(Nothing,Nothing)

End Sub

, click Button1 Button2 . .

, , . 2 subs, . , sub , , . Button Click , .

...

- , Button.Click, "" AddHandler.

Module Module1

Public Sub MySub()

Form1.TextBox1.Text = "Button Clicked"

End Sub
End Module

:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    AddHandler Button1.Click, AddressOf Module1.MySub

End Sub

Sub button.click, , .

, , .

+3

, - , , Form1.Button1.Click( - ) Form1Handler.

Form1 , , .

?

Form1.Designer.vb: :

Public Partial Class Form1
    ' ...
    Private Button2 As System.Windows.Forms.Button
    Private Button1 As System.Windows.Forms.Button
End Class

^^ Private Protected...

    Protected WithEvents Button2 As System.Windows.Forms.Button
    Protected WithEvents Button1 As System.Windows.Forms.Button

WithEvents, .

IDE (Visual Studio 2010 SharpDevelop) , () "". Form1.Designer.vb. , "WithEvents".

Form1Handler, Form1.

Form1Handler?

, Form1Handler.

Private Sub HandleButtonClick(sender As Object, e As EventArgs)
    ' Instructions for Clicks goes here...
End Sub

,

Private Sub AddHandlersToButtonClick()
    AddHandler Button1.Click, AddressOf HandleButtonClick
    AddHandler Button2.Click, AddressOf HandleButtonClick
    ' Add as many handlers as you wish, calling as many methods as you wish...
End Sub

, Form1Handler :

Private Sub OnForm1Handler_Load() Handles Me.Load
     AddHandlersToButtonClick()
End Sub

, Form1Handler, Form1, HandleButtonClick (..) , Button1 Button2. - , Button3, Panel1... :

    ' Creates as many methods you wish for each of your Form1 Controls,
    ' ensuring those controls are declared as Protected in Form1.
    Private Sub HandleButton1Click(sender As Object, e As EventArgs)
        ' ...
    Private Sub HandleButton2Click(sender As Object, e As EventArgs)
    Private Sub HandleButton3Click(sender As Object, e As EventArgs)
    Private Sub HandlePanel1MouseEnter(sender As Object, e As EventArgs)

    ' Then writes in AddHandlersToButtonClick() the dynamic Events handlers
        AddHandler Button1.Click, AddressOf HandleButton1Click
        AddHandler Button2.Click, AddressOf HandleButton2Click
        AddHandler Button3.Click, AddressOf HandleButton3Click
        AddHandler Panel1.MouseEnter, AddressOf HandlePanel1MouseEnter

, 300 ?

! , , , - , IDE:

  • Button1 → Tag = 1
  • Button2 → = 2
  • ...
  • Button300 → = 300

- :

Private Sub HandleButtonClick(sender As Object, e As EventArgs)
    Dim MyButton As System.Windows.Forms.Button
    Dim MyButtonIndex As Integer

    If TypeOf(MyButton) Is System.Windows.Forms.Button Then ' You should check it a Button.
        MyButton = CType(sender, System.Windows.Forms.Button) ' Option Strict On - Always !
        If Integer.TryParse(MyButton.Tag.ToString(), MyButtonIndex) Then
            Select Case MyButtonIndex
                Case 1:
                    ' Little instruction
                Case 2:
                    ' Call a bigger method, with newer parameters
                    Call HandleMainFormButton1(MyButton, "Houston we have a problem !")

                ' ...
                Case 300:
                    Call HandleMainFormButton300("Just kidding..!")
                Case Else
                    ' This is NOT an indexed Button.
                    MessageBox.Show("You've forgotten to handle button " + MyButtonIndex.ToString())
            End Select
        End If
    End If
End Sub

^^ CRAZY! 300+ № 300... 300 ...

AddHandler Button1.Click, AddressOf HandleButtonClick
AddHandler Button2.Click, AddressOf HandleButtonClick
AddHandler Button3.Click, AddressOf HandleButtonClick
' ...
AddHandler Button299.Click, AddressOf HandleButtonClick
AddHandler Button300.Click, AddressOf HandleButtonClick
+2

:

Button1.Click Form1?

, :

Public Partial Class Form1
    Private Sub Button1Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("Form1 : " + Button1.Text)
    End Sub

    Public Sub New()
        Me.InitializeComponent()
    End Sub
End Class

Public Class Form1Handler
    Inherits Form1

    Private Sub HandleForm1HandlerButton1Click(sender As Object, e As EventArgs)
        MessageBox.Show("Form1Handler : " + Button1.Text)
    End Sub

    Private Sub AddHandlersToButtonClick()
        AddHandler Button1.Click, AddressOf HandleForm1HandlerButton1Click
    End Sub

    Private Sub OnMainFormHandlerLoad() Handles Me.Load
        AddHandlersToButtonClick()
    End Sub
End Class

Button1 Form1, :

MainForm: Button1 [OK]

Button1 Form1Handler, :

: Form1: Button1 [OK]

: Form1Handler: Button1 [OK]

, Form1:

1:

Form1 Private Sub Button1 (...) .

    Public Sub Button1Click(sender As Object, e As EventArgs) Handles Button1.Click

Form1Handler:

    Private Sub AddHandlersToButtonClick()
        RemoveHandler Button1.Click, AddressOf Button1Click
        AddHandler Button1.Click, AddressOf HandleForm1HandlerButton1Click
    End Sub

, Button1Click (...) Form1 Form1Handler.

2: ( )

Button1 Form1, .

Public Partial Class Form1
    Protected Overridable Sub Button1Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("Form1 : " + Button1.Text)
    End Sub
    ' ...
End Class

Form1Handler:

Public Class Form1Handler
    Inherits Form1

    Protected Overrides Sub Button1Click(sender As Object, e As EventArgs)
        MessageBox.Show("Form1Handler : " + Button1.Text)
    End Sub
End Class

AddHandlersToButtonClick().

HandleForm1HandlerButton1Click().

, Button1Click, Button2Click,..., Button300Click; , - Form1Handler...

+1

Source: https://habr.com/ru/post/1743885/


All Articles