Assignment statements are very important, and we will use them in many ways. For now, let us see how they can be used to assign new values to properties during execution. Let us consider four examples.
1. This bilingual greeting program changes the value of the Text property of a label during execution. The program has handlers for two events, clicking on the "English" and "Spanish" buttons:
Private Sub btnEnglish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnglish.Click
lblOutput.Text = "Hello, my friend!"
End Sub
Private Sub btnSpanish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpanish.Click
lblOutput.Text = "Hola, amigo!"
End Sub
These event handlers use assignment statements to change the value of the Text property of the label called lblOutput. Clicking on the Spanish button assigns the value "Hola, Amigo!" to the Text property of lblOutput and clicking on English assigns it a value of "Hello, my friend!".
2. In our second example, the ForeColor property of lblGreeting is assigned the value red or blue depending upon which button the user clicks. The two event handlers are:
Private Sub btnRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click
lblGreeting.ForeColor = Color.Red
End Sub
Private Sub BbtnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BbtnBlue.Click
lblGreeting.ForeColor = Color.Blue
End Sub
3. Our third example displays or hides a label by assigning true or false to its Visible property.
Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
lblGreet.Visible = False
End Sub
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
lblGreet.Visible = True
End Sub
4. In our final example, the assignment statement is used to change the value of the Top property of a label.
Private Sub BtnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
lblGreeting.Top = 24
End Sub
Private Sub BtnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
lblGreeting.Top = 216
End Sub