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
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:
Open in new window
Form:
Open in new window
You can reduce code for the control in above example like the following.
Open in new window