frmExample.Caption = “This is a caption in quotes”

|
frmExample.Caption = “This is a caption in quotes”
|
bet | 3/3 | Sana | 25.12.2019 | Hajmi | 302 Kb. | | #4849 |
frmExample.Caption = “This is a caption in quotes”
This assignment statement says the Caption property of the form is replaced by (or changed to) the string value on the right side of the statement. You should now have some idea of how assignment statements work.
Comments
When we talked about project design, it was mentioned that you should follow proper programming rules when writing your BASIC code. One such rule is to properly comment your code. You can place non-executable statements (ignored by the computer) in your code that explain what you are doing. These comments can be an aid in understanding your code. They also make future changes to your code much easier.
To place a comment in your code, use the comment symbol, an apostrophe (‘). This symbol is to the left of the key on most keyboards, not the key next to the 1 key. Anything written after the comment symbol will be ignored by the computer. You can have a comment take up a complete line of BASIC code, like this:
‘Change form to blue
frmExample.BackColor = vbBlue
Or, you can place the comment on the same line as the assignment statement:
frmExample.BackColor = vbBlue ‘Makes form blue
You, as the programmer, should decide how much you want to comment your code. We will try in the projects provided in this course to provide adequate comments. Now, on to the first such project.
Project - Form Fun
Project Design
In this project, we will have a little fun with form properties using command buttons. We will have a button that makes the form grow, one that makes the form shrink, and two buttons that change the form color. We’ll even have a couple of buttons that make the other buttons disappear and reappear. The project you are about to build is saved as FormFun in your particular project folder.
Place Controls on Form
Start a new project in Visual Basic. Size the form so six command buttons will fit on the form. Place six command buttons on the form. Move the buttons around until the form looks something like this:
One warning. If you’ve used Windows applications for a while, you have probably used the edit feature known as Copy and Paste. That is, you can copy something you want to duplicate, move to the place you want your copy and then paste it. This is something done all the time in word processing. You may have discovered, in playing around with Visual Basic, that you can copy and paste controls. You might be tempted to do that here - why create six command buttons when you could just create one command button, then copy and paste it five times? Yes, you could do that, but don’t! If you do, it will look like you have command buttons on the form and you do, kind of. Copying controls gives you a different type of control - one you study in more advanced Visual Basic classes. So, in this class, we will always create single copies of every control we need. Later, as you become a better programmer, you might want to look into what is happening when you copy and paste controls.
Set Control Properties
Set the control properties using the properties window. Remember that to change the selected control in the properties window, you can either use the controls list at the top of the window or just click on the desired control. For project control properties, we will always list controls by their default names (those assigned by Visual Basic when the control is placed on the form).
Form1 Form:
Property Name Property Value
Name frmFormFun
Caption Form Fun
Command1 Command Button:
Property Name Property Value
Name cmdShrink
Caption Shrink Form
Command2 Command Button:
Property Name Property Value
Name cmdGrow
Caption Grow Form
Command3 Command Button:
Property Name Property Value
Name cmdHide
Caption Hide Buttons
Command4 Command Button:
Property Name Property Value
Name cmdRed
Caption Red Form
Command5 Command Button:
Property Name Property Value
Name cmdBlue
Caption Blue Form
Command6 Command Button:
Property Name Property Value
Name cmdShow
Caption Show Buttons
Visible False
You can change other properties if you want - maybe change the Font property of the command buttons.
When you’re done setting properties, your form should resemble this:
cmdShrink
cmdGrow
cmdHide
frmFormFun
cmdRed
cmdBlue
cmdShow
What we have are six command buttons, two to change the size of the form, two to change form color, one to make buttons go away, and one to make buttons reappear. Notice the Show Buttons command button has a Visible property of False. We don’t want it on the form at first, since the buttons will already be there. When we make the buttons go away (by changing their Visible property) by clicking the Hide Buttons control, we will make the Show Buttons button appear. Makes sense, doesn’t it? But, why is the Show Buttons button there if its Visible property is False? Remember a False Visible property will only be seen in run mode.
Write Event Procedures
We have six command buttons on our form. We need to write code for the Click event procedure for each of these buttons. We’ll also want to write a Click event procedure for the form - we’ll explain why. We have a button on the form that makes the form shrink. What if we shrink it so much, we can’t click on the button to make it grow again. We can avoid that by allowing a click on the form to also grow the form. This ‘thinking ahead’ is one of the project design concepts we talked about.
For each event procedure, you use the code window. Select the control in the object list and the event in the procedures list. Then click in the region between the header line and footer line and start typing code. It’s that easy. But, again, make sure you type in everything just as written in these notes. You must be exact!
First, let’s type the cmdShrink_Click event procedure. In this procedure, we decrease the form height by 100 twips and decrease the form width by 100 twips:
Private Sub cmdShrink_Click()
'Shrink the form
'Decrease the form height by 100 twips
frmFormFun.Height = frmFormFun.Height - 100
'Decrease the form width by 100 twips
frmFormFun.Width = frmFormFun.Width - 100
End Sub
Before looking at the other event procedures, let’s look a bit closer at this one since it uses a few ideas we haven’t clearly discussed. This is the event procedure executed when you click on the button marked Shrink Form. You should easily recognize the comment statements. The non-comment statements change the form height and width. Look at the statement to change the height:
frmFormFun.Height = frmFormFun.Height - 100
Recall how the assignment operator (=) works. The right side is evaluated first. So, 100 is subtracted (using the - sign) from the current form height. That value is assigned to the left side of the expression, frmFormFun.Height. The result is the form Height property is replaced by the Height property minus 100 twips. After this line of code, the Height property has decreased by 100 and the form will appear smaller on the screen.
This expression also shows why we call the assignment operator (=) just that and not an equal sign. Anyone can see the left side of this expression cannot possibly be equal to the right side of this expression. No matter what frmFormFun.Height is, the right side will always be 100 smaller than the left side. But, even though this is not an equality, you will often hear programmers read this statement as “frmFormFun.Height equals frmFormFun.Height minus 100,” knowing it’s not true! Remember how assignment statements work as you begin writing your own programs.
Now, let’s look at the other event procedures. The cmdGrow_Click procedure increases form height by 100 twips and increases form width by 100 twips:
Private Sub cmdGrow_Click()
'Grow the form
'Increase the form height by 100 twips
frmFormFun.Height = frmFormFun.Height + 100
'Increase the form width by 100 twips
frmFormFun.Width = frmFormFun.Width + 100
End Sub
The cmdRed_Click event procedure changes the form background color to red:
Private Sub cmdRed_Click()
'Make form red
frmFormFun.BackColor = vbRed
End Sub
while the cmdBlue_Click event procedure changes the form background color to blue:
Private Sub cmdBlue_Click()
'Make form blue
frmFormFun.BackColor = vbBlue
End Sub
The cmdHide_Click event procedure is used to hide (set the Visible property to False) all command buttons except cmdShow, which is made Visible:
Private Sub cmdHide_Click()
'Hide all buttons but cmdShow
cmdGrow.Visible = False
cmdShrink.Visible = False
cmdHide.Visible = False
cmdRed.Visible = False
cmdBlue.Visible = False
'Show cmdShow button
cmdShow.Visible = True
End Sub
and the cmdShow_Click event procedure reverses these effects:
Private Sub cmdShow_Click()
'Show all buttons but cmdShow
cmdGrow.Visible = True
cmdShrink.Visible = True
cmdHide.Visible = True
cmdRed.Visible = True
cmdBlue.Visible = True
'Hide cmdShow button
cmdShow.Visible = False
End Sub
Lastly, the Form_Click event procedure is also used to ‘grow’ the form, so it has the same code as cmdGrow_Click:
Private Sub Form_Click()
'Grow the form
'Increase the form height by 100 twips
frmFormFun.Height = frmFormFun.Height + 100
'Increase the form width by 100 twips
frmFormFun.Width = frmFormFun.Width + 100
End Sub
(Make sure you have the correct procedure here. When you choose the Form control, the procedure displayed will be Load. Choose the Click event using the procedures list.) Review the earlier-discussed techniques for saving a new project. Save your project.
You should easily be able to see what’s going on in each of these procedures. Pay special attention to how the Visible property was used in the cmdHide and cmdShow button click events. Notice too that many event procedures are very similar in their coding. For example, the Form_Click event is identical to the cmdGrow_Click event. This is often the case in Visual Basic projects. Unlike control placement, we encourage the use of editor features like Copy and Paste when writing code. To copy something, highlight the desired text using the mouse - the same way you do in a word processor. Then, select Edit in the Visual Basic main menu, then Copy. Move the cursor to where you want to paste. You can even move to other event procedures. Select Edit, then Paste. Voila! The copy appears. The pasted text might need a little editing, but you will find that copy and paste will save you lots of time when writing code. And, this is something you’ll want to do since you probably have noticed there’s quite a bit of typing in programming, even for simple project such as this. Also useful are Find and Replace editor features. Use them when you can.
VB5 and VB6 offer another way to reduce your typing load and the number of mistakes you might make. If you click Tools, then Options on the Visual Basic main menu, then select the Editor tab, there is an option called Auto List Members. If this option is selected, while you are writing BASIC in the code window, at certain points little boxes will pop up that display information that would logically complete the statement you are working on. Then, you can select the desired completion, rather than type it. If you use VB5 or VB6, you might want to try the Auto List Members option. Use on-line help to find out more about its use.
Run the Project
Go ahead! Run your project - click the Start button on the Visual Basic toolbar. If it doesn’t run properly, the only suggestion at this point is to stop the project, recheck your typing, and try again. We’ll learn ‘debugging’ techniques in the next class.
Try all the command buttons. Grow the form, shrink the form, change form color, hide the buttons, make the buttons reappear. Make sure you try every button and make sure each works the way you want. Make sure clicking the form yields the desired result. This might seem like an obvious thing to do but, for large projects, sometimes certain events you have coded are never executed and you have no way of knowing if that particular event procedure works properly. This is another step in proper project design - thoroughly testing your project. Make sure every event works as intended. Stop your project (click the Visual Basic toolbar Stop button). Save your project if you changed anything.
Other Things to Try
For each project in this course, we will offer suggestions for changes you can make and try. Modify the Shrink Form and Grow Form buttons to make them also move the form around the screen (use the Left and Top properties). Add more possible colors to the form using the other symbolic constants we defined. Change the Hide Buttons button so that it just sets the command buttons’ Enabled property to False, not the Visible property. Similarly, modify the Show Buttons button.
Summary
Congratulations! You have now completed a fairly detailed (at least there’s more than one control) Visual Basic project. You learned about project design, saving projects, details of the form and command button controls, and how to build a complete project. You should now be comfortable with the three steps of building a project: placing controls, setting properties, and writing event procedures. We will continue to use these steps in future classes to build other projects using new controls and more of the BASIC language.
This page intentionally not left blank.
|
| |