With your previous project open, click the menu Project > Add Windows Form
Make sure that the Windows Form icon is selected.
In the Name box, type frmHideShow.
Press Enter or click Open.
Notice that the form is named frmHideShow, indicated by the (Name) property in the properties list
In the Properties window, set the FormBorderStyle property to Fixed Single, then set the MaximizeBox property to False.
In the Text property, type Hide & Show, or whatever you want
If some of your windows were not there, go to the menu View > Properties Window. Select others you need or want as necessary
Now we'll add some objects.
Double-click the command button in the toolbox, then move it down a bit and to the left.
Add two more command buttons and position them to the right of the first.
Double-click the label control in the toolbox twice to add two labels.
Position them a few spaces above the end command buttons.
Enter these names appropriately for the objects:
lblLeft
lblRight
Left most command button: cmdShow
Middle command button: cmdHide
Right most command button: cmdExit
Set the command buttons captions as follows based on their name using their Text property:
&Show
&Hide
&Exit
Recall that the & sets the following letter in the caption property to an access key.
Select each label separately and erase the caption in them
Set their AutoSize property to True This property makes the labels appear much more attractive in the development environment (as opposed to run-time environment) by sizing the label according to its caption and font size.
Speaking of font size, select each label separately and set their font size to 24. Use the Font property to do so. Click on the ellipsis(...), then choose 24 in the size list. Click OK.
Now set the caption property for the labels to "Left" and "Right" appropriately
As you can probably imagine right now, I will teach you to hide and show these labels. Therefore it does not make sense to show a label when the user can already see it. Therefore:
Select the cmdShow command button and set its Enabled property to False.
This property determines whether it can respond to user events. Enabled False means that it cannot respond. You will notice at runtime you cannot click the button at all.
Before I forget, click on the project icon in the Solution Explorer window. The icon above the Forms folder. You will see that the properties list is now limited to the (Name) property.
Type in Program or whatever you want, but without spaces. Spaces are invalid in the (Name) property.
Before we right some code, here are some notes on the code window:
The upper-left drop-down box is called the object box, the same as the one in the properties window. It lists all the objects in the form you are using.
The box to the right is called the event box. It lists all the available events to the programmer that the selected object can use.
Another note: Good programmers always use comments. This will make my life much easier as well, because now you can just type the comments I provide when you write your code to ensure your understanding.
A comment is made with the ' mark, or apostrophe mark. They can be placed above and below code, as well as to the right of the line. I am not aware of any other places, but you will not need to use them elsewhere. Comments exist in every language that I know of.
Example: 'show the left label to the user.
To make a multi line comment, you use the underscore key( _ ). For example:
'This is the first line of my comment
_
This is the second line of my comment _
This is the last line of my comment
The underscore key also tells the code to treat the next line as part of the code written on the line above. This is can be very useful to programmers if they have a very long line of code to write. Traditionally, these are not multi-line comments, but the use of the underscore as seen above lets the comment expand to the next line.
Now lets write some code.
<--
OBJECT BOX
Double-Click on cmdExit and type End in the code window. You will be placed in the right place after double-clicking the button on the form.
Select cmdShow in the code window object box (the drop-down box that shows the current object you are coding seen above). Type the following lines:
lblLeft.Visible = True 'show the left label to the user
lblRight.Visible = True 'show the right label to the user
cmdShow.Enabled = False 'disable the command button cmdShow
cmdHide.Enabled = True 'enable the command button cmdHide
Now select the cmdHide object in the code window object box. Type the following lines:
lblLeft.Visible = False 'hide the left label from the
user
lblRight.Visible = False 'hide the right label to the user
cmdShow.Enabled = True 'enable the command button cmdShow
cmdHide.Enabled = False 'disable the command button cmdHide
To understand this code, refer to the comments on each line. Remember: Object.Property. That is how the properties are set to something in coding.
The one other thing to understand about this code is that you need to look at the structure of the code window. Notice at the very top of the code window is the line Public Class frmHideShow. This line simply declares a class called frmHideShow, and gives the class a public scope. The keyword Public determines the scope. This allows other classes to communicate with this form. If the keyword were Private instead, the form becomes unusable to objects which are not a part of it. The keyword Class simply means that this form object consists of objects. A class is simply a group of objects. This form contains objects, such as the ones we added such as the label, text box etc. Because this form is made up of objects, it is called a class.
Notice how the code window separates each object on the form. Each command button has its own sub-procedure. A sub-procedure is simply a set of commands. The code we entered previously controls two different sub-procedures--
cmdHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHide.Click
AND
cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click.
Let's just focus on the command button cmdHide. All this code is doing is setting up a sub-procedure (a set of commands) cmdHide. This code is called when the button cmdHide is clicked (set up at end of line Handles cmdHide.click). The underscore click (cmdHide_Click) is just the name of the object. Since we can code several different events for this command button, the "_Click" is added to create a different name for each sub-procedure in order to avoid conflict.
Moving on now...
Lock the controls on the form.
Recall:
Right-click on the form
Click Lock controls.
If it does not work, make sure that each object's Visible property is set to True in the properties list.
Well, time to move on to Page 4.