Forms in Visual Basic
Go back to form1. Double click on btnClouds. Modify the Click event to handle all 4 buttons. Notice that the type for sender was changed to Button:
Private Sub Btn_Click(ByVal sender As Button, ByVal e As System.EventArgs) _
Handles BtnClouds.Click, BtnRain.Click, BtnSnow.Click, BtnSun.Click
Dim Info(5) As String
Info(0) = "Clouds: lots and lots of clouds"
Info(1) = "It snowed for days on end."
Info(2) = "A beautiful day to work on the computer."
Info(3) = "Take your umbrella and wear your boots!"
Dim Frm As New FrmWeather()
Frm.BackColor = sender.BackColor 'See A below
Frm.Text = sender.Text 'See A below
Dim Index As Integer = sender.Tag 'See B below
Frm.LblInfo.Text = Info(Index)
Frm.Pic.Image = sender.Image
Frm.Show() 'See C below
End Sub 'Btn_Click
A.) When an event handles more than one control, the sender argument is used to determine which of the controls the event occurred for. In this instance, we don’t need to know which of the buttons was clicked, we simple use the background and text from sender and use it to change properties of frm.
B.) We gave each of the buttons a tag numbered 0, 1, 2, 3. We retrieve the tag from sender and use it as the index to an array. The array info is used to put appropriate text into the label on frm.lblInfo.
C.) The show method displays the form no-modally. You can click all of the buttons, or the same button several times and they all are displayed until you close them individually, or until you close the main form.