Avatar of mondintator
mondintator
 asked on

VB.net User Control button press raise event to parent form

Hi Experts,

I am a complete novice in vb.net.  I have a form with a panel within which sits several user controls.  Depending on what activity the user is performing, a different user control is displayed in the panel.  No problems.

However, I would like one of the user controls to have 6 buttons, that when pressed, the master form can understand which button has been pressed.

I tried to add an Event to the User Control but it says the handles clause requires a WithEvents variable defined in the containing type or one of its base types.  But I couldn't get that working so I've come to you for some help.

UserControl:
Public Class UserControl1

    Public Event S1_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Public Sub ButtonG2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonG2.Click
        RaiseEvent S1_Button_Click(sender, e)
    End Sub

End Class

Form:
Public Class Form1

    Dim Screen1 As UserControl1
    Dim Active_screen As System.Windows.Forms.Control

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

        Screen1 = New UserControl1
        Panel1.Controls.Add(Screen1)
        Active_screen = Screen1

    End Sub

    Private Sub Screen1_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Screen1.S1_Button_Click
            MsgBox("Button pressed!") <----- Here I want to understand which button was pressed but I've put this to just get it going
    End Sub

End Class
Visual Basic.NETSystem Programming

Avatar of undefined
Last Comment
bansidhar

8/22/2022 - Mon
bansidhar

you should use
Dim WithEvents Screen1 As UserControl1 ' You need WithEvents keyword for more help on this WithEvents (Visual Basic)


If you just want to get the name of the button, I would suggest another approach. Use simpler event in user control.
UserControl:
Public Class UserControl1

        Public Event Button_Click(ByVal buttonName As String)

        Public Sub ButtonG2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonG2.Click
            RaiseEvent Button_Click("ButtonG2")
        End Sub
        Public Sub ButtonG1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonG1.Click
            RaiseEvent Button_Click("ButtonG1")
        End Sub
        Public Sub AnotherButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnotherButton.Click
            RaiseEvent Button_Click("AnotherButton")
        End Sub
End Class

Open in new window


Form:
Public Class Form1

    Dim WithEvents Screen1 As UserControl1
    Dim Active_screen As System.Windows.Forms.Control

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

        Screen1 = New UserControl1
        Panel1.Controls.Add(Screen1)
        Active_screen = Screen1

    End Sub

    Private Sub Screen1_Button_Click(ByVal buttonName As String) Handles Screen1.S1_Button_Click
            MsgBox(buttonName & " pressed!") <----- Here I want to understand which button was pressed but I've put this to just get it going
    End Sub

End Class

Open in new window


You can reduce code for the control in above example like the following.

Public Class UserControl1

        Public Event Button_Click(ByVal buttonName As String)

	'Handling multiple buttons
        Public Sub ButtonG2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonG2.Click, ButtonG1.Click, AnotherButton.Click
            RaiseEvent Button_Click(DirectCast(sender, Button).Name)
        End Sub
 
End Class

Open in new window

mondintator

ASKER
Thank you!  That gets the temporary message to work, but what is the correct syntax for passing which button has been pressed.  UserControl1 has 6 buttons - how does form1 know which button has been pressed?
ASKER CERTIFIED SOLUTION
bansidhar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jacques Bourgeois (James Burger)

I would not call the Event Button_Click, but rather ButtonClick. This would follow the conventions and prevents possible ??? when coming back in the code later and see something like Screen1_Button_Click. By convention, the name of an event method has only one underscore.

Right click on the event name and select rename, this will rename it everywhere it is used in the project.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
bansidhar

I agree, I actually don't like the underscores in the function names. I use the full camel casing convention (too much addicted to resharper). I made only minimal changes to the original code, not to confuse the Author.