Alignment Program
Changing Properties at Design-Time and Run-Time
Step 1: Design the Form, and set initial properties of objects (Design-Time).
-
Open a new VB Project
-
Name the form frmAlignment
-
Change the Size of your form to 500, 250
-
Add 4 Buttons to the form, and place them in a straight line along the bottom of your form.
-
Assign the following properties to the buttons:
-
Button1 – Name: btnLeftAlign Text: Left
-
Button2 – Name: btnCenterAlign Text: Center
-
Button3 – Name: btnRightAlign Text: Right
-
Button4 – Name: btnExit Text: Exit
-
Add a Label to the form. Change the Name of the label to lblMessage. Change the AutoSize Property to False. Resize the label so that it is as wide as your form and a couple of lines deep. Change the Text Property so that the label shows nothing.
Step 2: Write the code that will make the different things happen when different events happen while the program is running. (Run-Time)
-
Write the code for the click event on the btnLeftAlign button. Insert the lines of code shown below. These are the statements that will be executed when the program is running and the btnLeftAlign button is clicked.
Private Sub btnLeft_Click(ByVal … ) Handles btnLeft.Click
‘changes the labels message and alignment
lblMessage.Text = “This text is aligned on the left.”
lblMessage.TextAlign = ContentAlignment.TopLeft
End Sub
Tip **Notice that after you type the . and the = options are listed. You can click on the one you want to select it instead of typing everything out!! **
-
Insert the code into the click event procedures for the other command buttons, programming them to change the properties of lblMessage at run-time as necessary.
-
Insert the code for the btnExit button. (application.exit)
Form _Load Event Procedure
When the program executes the first thing it does is load the form and all of its objects into memory. Often we want to program things that happen at this point – initial instructions that only happen at the beginning of the program. To do this, we can put statements in the Form_Load Event procedure.
In the alignment program, we could program the caption of the label to initially say “Watch My Alignment Change!”. We can set properties in the Form_Load Event procedure as follows:
Private Sub frmAlignment_Load(ByVal …) Handles MyBase.Load
'setting the initial properties of the label
lblMessage.Text = "Watch My Alignment Change!"
lblMessage.TextAlign = ContentAlignment.BottomCenter
End Sub
-
Run your program, and notice the initial message in the label.
-
Add instructions in your Form_Load event that will initially set the font colour (ForeColor) the way you want.
Enrichment: Investigate the changing of other properties at run-time. Make a list of the ones that you have learned out to program.
Share with your friends: |