We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2015-09-22
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.

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

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
        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
Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE