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

mondintator
mondintator used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

Commented:
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?
In
RaiseEvent Button_Click("ButtonG2") '"ButtonG2" is passed to the event handler as parameter.

Open in new window

if you want to pass the button number as integer from 1 to 6 you can modify the event handler as
Public Event Button_Click(ByVal buttonID As Integer) ' the name buttonID  doesn't matter you can use any name here

Open in new window

and on each button press of the control you can raise event like
       'Assuming ButtonG1 is your first button and ButtonG2 is your second button
       Public Sub ButtonG1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonG1.Click
            RaiseEvent Button_Click(1)
        End Sub

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

Open in new window

and in form
Private Sub Screen1_Button_Click(ByVal buttonID As String) Handles Screen1.S1_Button_Click
    Select Case buttonID
        Case 1
            ' button 1 was clicked
        Case 2
            ' button 2 was clicked etc.....
   End Select
End Sub

Open in new window

Hope this clarifies.
Top Expert 2015

Commented:
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.
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial