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
mondintatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bansidharCommented:
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

mondintatorAuthor 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?
bansidharCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
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.
bansidharCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.