VB entry tutorial - Chapter 1 Hello World

xiaoxiao2021-03-06  17

This article is from: http://www.docsky.com/show.asp? Id = 2068 a total of four chapters will be posted, please pay attention to the first chapter Hello World! People who have not learned programming often ask, it is difficult to program This problem is difficult to answer. Difficult, it is difficult to make a good program, especially in this, and some programs should also use some other professional knowledge, such as a graphics handler, you have to understand the format of the image, even some art knowledge. But the programming is also easy. Take VB, and a self-entertainment little tool is still a quite pleasant thing. Of course, it can't completely replace the artificial, for example, complete your homework, launch to school Missile ... As a fourth-generation computer language, VB is characterized by apparent. Because it is "visible", we may wish to see the VB interface (Figure 1_1) (I use VB4.0 32 version), it is easy to see, there are five windows on the screen, (1) is Menu bar; (2) is a toolbox (Tools box), which is the control required by VB; (3) is a form (Form) to be designed by the VB program; (4) Properties of the control (Properties), Each control in VB has its own properties, such as the text box has a text property, and any control has a single name, which makes we complete some programming work without programming; (5) is VB The file control window, each program of VB is called a project, and one project file often includes the following two files, forms (FORM) files, and module (BAS) files, of course there are Class files, these files It makes up a complete program, and the project file (VBP) is used to describe the contents included in the program. In fact, I have always thought that the best way to learn is to actually operate, well, let's take a look at the simplest program, like all programming textbooks, we call the first program "Hello, World!" . First, let's first familiarize with the form. If you have used the drawing board with Win95 attachments, then you will be familiar with the VB's form design method. Click any of the toolbox with your mouse, and then drag the mouse on the form. You will get a required control; of course, there are other methods to get controls, you can double-click to get a control on the toolbox; or hold down the Ctrl key, then click the desired control, repeat the first step You will find that you don't have to go back to click the toolbox, and right click to restore the mouse pointer, which is especially useful for the same control of the repeat painting. For our first applet, you know that these are already enough. Now you can draw a picture box (Picture Box) on our form, used to display the drawing, when each control is established, the system will set a default name (name), this PictureBox name is Called with Picture1, for convenience, we will use the default name.

Select Picture1, find the Picture property in the attribute window on the left, click here, click on Picture1 to select an image file, the image file supported by VB has three kinds of Bitmap, WMF, ICO, Microsoft provides a set of icon files for users. To store the ICONS directory of the VB directory, select the ICONS / MISC / FACE01.ICO file so that the icon is loaded with Picture1. Select the autosize property of PICTRUE1, set to 1-True, will find that Picture1 is as big as the image; set the BorderStyle to 0-None, remove the picture border. Add two picture boxes with the same method: Picture2 (Picture: Face02.ico), Picture: Face03.ico. The picture box is completed, step 2, established three wireless buttton, because it is named when it is called radio button of the radio), and the Caption is set to "Sorrow", "Happy" "Did not cure", placed in Figure 1_2. Finally, we also need three text boxes, set their text attributes to "" (empty string), and the last interface of the program is shown in Figure 1_3, so press the runtime window of the menu window, run the program, OK, but now the program is still unable to work, even if VB can't help, you must also enter the code to operate the program. VB is a typical object-oriented programming language, and its entire program is driven by events. So what is the event? In fact, our lives are full of a variety of incidents. For example, the incident of "Red Light" will cause the police to be chased by the police, and it will hurt when walking into the electric wire. The above example is a bit funny, but it can clearly explain the meaning of the event, just what will happen when you can immediately click on where the mouse is clicked, what will happen when pressing the keyboard, this is an event, and Event is so easy. To enter the VB code design area, you can get such a window with just double-click the mouse on the required control or form. As shown in Figure 1_4, each VB program consists of several small programs, which makes it easier to program the VB programming, divided into "procedure" and "function", both of which can pass parameters, The difference is that the function has a return value, and the process is not, only this. Double-click any part of the form, there will be an empty process: "Form_Load ()", this is a very important process, the event it driven is the event that the form is loaded, and a computers are like a computer. AutoExec.bat starts at startup. We have to make some settings for the controls in this program. We have just set up controls, but that is in program settings, sometimes we need to change the properties of certain controls in the program, through program code. VB specifies that a control property is access method: Name. Properties, what we set up for this program: private sub flow_load ()

Picture1.Visible = FalsePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = FalseText2.Visible = FalseText3.Visible = FalseEnd SubPrivate represents this process is local, Sub represents a start sub-process, as opposed to the process represented End Sub At the end, the visible property represents the visibility of the control, and this property has two True and False.

note:

The case is not distinguished in case, and the name has been defined, and the system will automatically turn it into the corresponding uppercase or lowercase.

tips:

1. The rules of variables and constants in VB.

Variables are generally the format of ABC, constant is ABC, and these variables and constant names may not be the same as keywords in VB, and some special symbols are not used, and these we will introduce, pay attention to develop good habits in learning. Make the program for yourself and others' browsing.

2. Tips for writing:

Don't really finish the program, use shortcuts: copy Ctrl C, cut Ctrl T, paste Ctrl P.

Press F5 and run the program, and now I find that there is only the wireless button left. Figure 1_5.

Private sub flow_load ()

Picture1.visible = falsePicture2.visible = falsePicture3.visible = falseText1.visible = falseText2.visible = falsext3.visible = false = falset3.visible = falseOption4.LEFT = -2000Option4.Value = True

End Sub

The LEFT property indicates the distance from the left frame of the control, set to -2000, and this control will not be seen in the form and make it = true. Run the program, everything is, as shown in Figure 1_6.

Note: Why don't you use option4.visible = false?

Because once the wireless button is set, it is not visible, then the value of it is really meaningless, do not believe it yourself.

Below we can continue. In the code window just now, select Object to Option1, and PROC is selected as Click, which is the process of obtaining an Option1_Click. code show as below:

Private subbotion1_click ()

Picture1.visible = truePicture2.visible = falsepicture3.visible = falseText1.visible = truetext2.visible = falseText3.visible = falsext1.text = "Goodbye, cruel world."

End Sub

Establish Option2_Click and Option2_click:

Private subboption2_click ()

Picture2.visible = truePicture1.visible = falsePictTure3.visible = falseText2.visible = truetext1.visible = falseText3.visible = falsext2.text = "Today is good." End Sub

Private subbotion3_click ()

Picture3.visible = truePicture2.visible = falsepicture1.visible = falsext3.visible = truext2.visible = falseText1.visible = falseText3.text = "Hello, World."

End Sub

Ok, our work is completed! Press F5 and play it. Although this program is simple, it has also been used for many basic knowledge. Do you start interested? ^ _ ^

Related Attachments: Hello.rar

This article is from: http://www.docsky.com/show.asp? ID = 2068 a total of four chapters will be posted, please pay attention

转载请注明原文地址:https://www.9cbs.com/read-41596.html

New Post(0)