VB Introductory Tutorials-Chapter One Hello World

Source: Internet
Author: User
Tags format constant empty
Tutorials | Getting Started Tutorial first chapter HELLO world!

People who have not studied programming often ask that programming is difficult, and that the question is difficult to answer. Difficult, indeed difficult, to make a good program especially, and some programs also need to use some other professional knowledge, such as a graphics processing program, it is necessary to understand the format of the image, or even some art knowledge; but programming is also very easy, take VB, It's a pretty cozy thing to make up a little gadget for fun, but it's not going to be a complete substitute for labor, such as completing your homework, firing missiles at school ...

As the fourth generation computer language, the characteristic of VB is obvious. Because it is "visual" so we might as well first look at the VB interface (Figure 1_1) (I use the VB4.0 32-bit version), it is easy to see that there are five windows on the screen, (1) is the menu bar, (2) is the Toolbox (Tools Box), which is the control required by VB (3) is a VB program to design the form (form), (4) Control and Form properties, in VB, each control has its own properties, such as text box has an attribute, and any one of the controls have their own unique name, This allows us to complete part of programming without programming; (5) is a VB file Control window, VB every program called a Project (project), a project file often includes the following two kinds of files, form (form) files and module (Bas) files, of course, and class files , these files form a complete program, and the project file (VBP) is used to describe what the program contains.



In fact, I always thought that the best way to learn is to actually do it, OK, let's take a look at one of the simplest programs, as with all programming textbooks, we call the first program "hello,world! ”。

First, we'll familiarize ourselves with the form. If you've ever used a drawing board with a Win95 attachment, then you will soon be familiar with VB Form Design method, with the mouse click on any one of the Toolbox control, and then drag the mouse on the form, you will get a required control; Of course, there are other ways to get control, You can double-click on the toolbox to get a control, or hold down the CTRL key, click the control you want, repeat the first step, you will find that you do not have to go back to click the Toolbox, right-click to restore the mouse pointer, which is particularly useful for repeatedly painting the same control.

For our first little program, it's enough to know that. Now you can draw the following controls on our form (picture box), to display the picture, each control is established, the system will give it a default name (name), the name of the PictureBox is called Picture1, for the sake of convenience, Our future controls use the default name. Select Picture1, in the left side of the property window to find the Picture property, click will appear, click for Picture1 Select an image file, VB support image file has bitmap, WMF, ico three kinds, Microsoft provides users with a set of icon files, stored in the VB Directory icons directory, select the Icons\misc\face01.ico file, so that the icon is loaded into the Picture1. Then select the Pictrue1 AutoSize property, set to 1-true, you will find that Picture1 to become as large as the image, BorderStyle as 0-none, remove the picture border. Add two more picture frames in the same way: Picture2 (Picture:face02.ico), Picture3 (Picture:face03.ico).

The picture frame is complete, the second step, the establishment of three wireless button (Option button), because it is like the radio button and the name of their name, the label of their properties (Caption), respectively, as "sad", "Happy", "no", put into a figure 1_2 appearance.



Finally we need three text boxes, respectively set their Text property to "" (empty string), the final interface of the program, such as Figure 1_3, so press the menu window run the button, run the program, but now the program can not work, even if it is VB can not exception, You must also enter code to make the program work.



VB is a typical object-oriented programming language, its entire program is driven by events. So, what is an event? In fact, our life is full of a variety of events, such as "red light" this incident will lead to be hunted by the police, walking hit the telegraph pole will hurt. The above example is a bit funny, but it can clearly explain the meaning of the event, just as you can immediately see what happens when you click the mouse, what happens when you press the keyboard, this is the event, and the event is so easy.

To enter the code design area of VB, you can get such a window simply by double-clicking the mouse on the control or form you want. As shown in Figure 1_4, each VB program is composed of a number of small programs, this makes VB programming easier, these small programs, divided into "process" (Procedure) and "Functions" (function), both can pass parameters, the difference is that the function has a return value, and the process does not, nothing more.



Double-click any part of the form to have an empty procedure: "Form_Load ()", which is a very important process, and it drives an event that the form is loaded as an analogy, like a batch program (Autoexec.bat) when the computer starts up. In this program we want to make some settings for the control. We've just set up the controls, but that's in the program settings, and sometimes we need to change the properties of certain controls in the program, through program code. VB stipulates that a control property access method: Name. property, so we set the control of this program as follows:

Private Sub Form_Load ()

Picture1.visible = False
Picture2.visible = False
Picture3.visible = False
Text1.visible = False
Text2.visible = False
Text3.visible = False

End Sub

Private means that this procedure is local, and the sub represents the beginning of the child process, in contrast to the end Sub representing the ending of the procedure, the Visible property represents the visibility of the control, and the value of this property is two true and false.

Attention:
VB in the case is not differentiated, and, the defined name, later used, the system will automatically turn it into the corresponding uppercase or lowercase.

Small knowledge:
The rules for variables and constants in 1.VB.

Variable is generally the format of ABC, the constant is ABC, and these variables and constant names must not be the same as the keyword in VB, also can not use some special symbols, which we will introduce in the future, attention in learning to develop good habits, so that the program to facilitate their own and others to browse.

2. Tips for writing Programs:

Do not really a sentence of the program finished, using shortcut keys: Copy Ctrl + C, cut ctrl+t, paste ctrl+p.

Press F5, run the program, and now found that only the wireless button is left. As shown in Figure 1_5.



 

Private Sub Form_Load ()

Picture1.visible = False
Picture2.visible = False
Picture3.visible = False
Text1.visible = False
Text2.visible = False
Text3.visible = False
Option4.left =-2000
Option4.value = True

End Sub

The Left property, which is set to 2000, is not visible in the form and is value=true when the control is at a distance from the border of the form. Run the program again, all the wishes, such as Figure 1_6.



Note: Why not option4.visible=false?

Because, once set the wireless button is not visible, then make its value is really meaningless, do not believe yourself to try.

Now we can go on. In just the code window, the object selection for Option1,proc Select as click, that is, to get a option1_click process. The code is as follows:

Private Sub Option1_click ()

Picture1.visible = True
Picture2.visible = False
Picture3.visible = False
Text1.visible = True
Text2.visible = False
Text3.visible = False
Text1.Text = "Goodbye, Cruel World." "

End Sub

The same established Option2_click and Option2_click:

Private Sub Option2_click ()

Picture2.visible = True
Picture1.visible = False
Picture3.visible = False
Text2.visible = True
Text1.visible = False
Text3.visible = False
Text2.text = "In a good mood today." "

End Sub

Private Sub Option3_click ()

Picture3.visible = True
Picture2.visible = False
Picture1.visible = False
Text3.visible = True
Text2.visible = False
Text1.visible = False
Text3.text = "Hello,world." "

End Sub

Well, we've finished the mountain! Press F5, play it. Although this program is simple, but also used a lot of basic knowledge, you are interested in it? ^_^



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.