Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I process a form's events in another class / module automatically in VB.NET?

Here's 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'm trying to get Form1Handler to process Form1's events automatically. How can I do this? Should I use a module instead? I'm doing this in Visual Basic 2010.

I don't want to have to make an event handler in Form1 and then pipe it to the other class / module. Is there some way to automatically "pipe" the events from form1 to form1handler?

like image 428
SteveGSD Avatar asked Jan 19 '26 00:01

SteveGSD


1 Answers

If you need to handle a button or other control events outside the form class, you can make a user control to inherit the control, and then handle the events in the user control class. For example, this user control consists of an internal button control named 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

You can use it in the main form like this:

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

The click event is handled inside the mb class.

like image 54
xpda Avatar answered Jan 20 '26 18:01

xpda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!