VB6 language basics
After preparation, we can start programming. For the general application development process in Visual Basic 6, the interface is designed first, and then functional code is written.
In the previous introduction, after creating a project (such as "Project 1"), a form Form1 is automatically created. In the example in this chapter, we will start to design this form. You can save this program while doing it, and we will continue to use it.
Next I will start the first step of interface design.
1. Add controls to the form
Here, we will first introduce some controls that are self-contained in VB6. By default, they are located on the left of the VB6 integrated development environment. This one is called a "toolbox ", just like an electrician, a car modifier, a student, or a designer, we place commonly used tools (Controls) in the toolbox (maybe just a stationery box) and take them out during use, how do we place these controls on the Form? There are two methods in Visual Basic 6.
First, double-click a control in the toolbox. In this case, a control of this type is displayed on the form, and its name is "control type + Serial Number" by default ", here we first use the default names of these controls. You can try it, for example, adding three TextBox, namely Text1, Text2, and Text3.
The second method is to click a control in the toolbox and draw a control over the form. The control created in this method is the same as the first one, the names are both of the default type + serial number.
Now, no matter which method is used, we need to create four controls on the form, namely, three text boxes and a button (CommandButton), and name it Command1. Then, we can display them better. please sort them in the order from Text1 to Text3 from top to bottom.
2. Set Control Properties
In the design phase, we can set the properties of some controls. When we select one or more controls, the property window on the right of the integrated development environment will display configurable properties. Here we can set the required values for these controls.
If no control is selected, you can also select the control to be set in the drop-down list of the Properties window. In this list, controls are listed in alphabetical order by the Control Type Control name.
Now, we set the Text attributes of the three Text boxes (Text1, Text2, and Text3) to empty strings respectively. At this time, on the form, we can immediately see that the name displayed in the text box is missing.
For the Command1 control, we set its Caption attribute to "computing", and we can immediately see the effect after setting on the form.
The position and size of the form and control are generally set by the Top, Left, Height, and Width attributes, we will find that their values can be set very large, so what are their units. In VB6, the default unit is Twip. For our commonly used unit pixels, a pixel is about 15 Ti, that is, if we want to set the form to 800 pixels, the Width attribute value should be set to 12000.
3. Run the program for the first time
After setting our first interface, you can't wait to run it. In the integrated development environment, press the "F5" key to see if the program is running. The window displayed on the screen is the one we just designed, but the grid does not exist during running.
Of course, this program can't do anything. (every program has to write a lot of code at the end of the interface to complete the required work.) Next, let's do something about it, that is to say, we have to start writing code.
4 text (string)
Now, double-click the Command1 button to see if we have the code to write. Now, refer to the following code and write a line of code here:
Private Sub commandementclick ()
Text3.Text = Text1.Text + Text2.Text
End Sub
Of course, we don't need to write any more of the existing parts in the code window. In the code, we can see that the Text attributes of the three Text box controls are used. Here, we want to display the sum of the content in Text1 and Text2 in the Text of the Text3 control. When calling the properties of a control, we use the format of "Control name. property name". The control name and property name are separated by dots of the English half-width characters.
Press F5 again to run the program. In the window, enter some numbers in Text1 and Text2, and click Command1 to see what the result will be?
Don't be surprised. The content displayed in Text3 must not be the sum of the two numbers we want. Why?
When introducing the concept of attributes, we have mentioned that for a Text box, its Text attribute is a string, and the "+" operator is used to add two strings, generally, two strings are connected, instead of adding them as numbers.
In VB6String.
From here, you may have thought that there should be differences in data types in programming, such as numerical values and strings. Now we have seen the string. Next, let's take a look at the value type in VB6.
5 Value Type
In Visual Basic 6, common numeric types include Integer, Long, floating-point, Double, and Currency) and Byte ).
There is no need to feel dizzy with so many data types. In fact, they only have some differences in storage space occupation and processing accuracy. The currency type occupies a large amount of storage space and uses 8 bytes to store data, but it has the highest processing precision. The integer and long integer types can only be used to process integers without decimal places, the integer type uses two bytes to store data, while the long integer type uses four bytes to store data (it can certainly process larger integers). The floating point type can have decimal places if the Double Precision Floating Point type is used, however, its operation accuracy is not very accurate, so it is generally not used for scientific, financial, and other operations. I rarely use them in this book, but it should be noted that, in VB6, the position and size values of the control are of the floating point type (Single), and the last value type is of the byte type. That is to say, all data of this type uses one byte (eight bits).
Now, we need to improve the previous program so that it can correctly add two numbers.
Please change the code above to the following format:
Private Sub commandementclick ()
'Text3. Text = Text1.Text + Text2.Text
Dim n1 As Currency
Dim n2 As Currency
Dim sum As Currency
N1 = CCur (Text1.Text)
N2 = CCur (Text2.Text)
Sum = n1 + n2
Text3.Text = CStr (sum)
End Sub
Next I will explain the new situation in the code.
The three statements Dim n1 As Currency, Dim n2 As Currency, and Dim sum As Currency define the three Currency data n1, n2, and sum respectively. We call them Currency variables, later, we will use them to store currency data.
From the statement, we can see that Dim, As, data type identifier (Currency) and Data variable name are used for variable definition, they are separated by spaces. Dim and As are called keywords, while Currency is called system reserved words. They and variable names (such As n1, n2, sum) are collectively referred to As"Identifier".
The definition of a variable name generally consists of letters, numbers, and underscores. Here I will start with a letter. Note that when defining a variable, do not show identifiers that are the same as keywords, system reserved words, and system method names. Otherwise, unexpected errors may occur. You can use Chinese characters as variable names, for example:
Dim good As String
Good = "Hello! "
MsgBox is good
However, we do not recommend that you use Chinese characters as variable names.
Back to the code above, after defining three currency variables, we use the statement "n1 = CCur (Text1.Text )", it means that the content of Text1 is converted into currency data and then assigned to the n1 variable. The "CCur" is a system method, because it can return a value (in this case, the data of the currency type), we call it"Function)", Which is actually another method type"Subroutine (sub)The only difference is that a function can return values, but a subroutine does not return values. In the brackets after the function name CCur, we call it"Parameters", The parameter put here is the content of Text1 (Text attribute value ).
The above "MsgBox" method is actually a function, but we didn't use its return value, so it is the same as the call of the subprogram without using parentheses, the parameter is written after the function name (of course, it must be separated by spaces ).
In addition, the method we used previously is a parameter. If there are multiple parameters, we should separate them by commas (half width.
We will introduce in detail how to define your own subprograms or functions.
After two conversion statements, we obtain the values of the two integers in Text1 and Text2. Then, in the statement "sum = n1 + n2, we assigned the value of the two Addons to another variable sum.
In the last input code "Text3.Text = CStr (sum)", you can easily see its meaning. Through the system function "CStr", we convert the calculated sum value to the String type, and then assign the result to the Text attribute of Text3, the calculation result is displayed in the text box Text3.
After learning about the code, we can run the program and press F5 to run the program.
In the window, please enter the correct value; otherwise, errors may occur, but do not fear, because errors will accompany our program for a lifetime. If an error message appears, you can click "end" to end the running of the program. However, if you enter two values, there should be no error.
Think about why something went wrong? It is very simple, because if the input content cannot be correctly converted to a value, what will happen? The program certainly does not know how to handle it, so it chooses to throw errors and how to handle these errors, we will learn later, but now we need to enter the correct number to check the program running (if you enter a large value, the program will make an error, this is because the data processed in the computer has a limited range, but generally its size is enough ).
After entering a value in Text1 and Text2 respectively, click the Command1 button to check whether the correct calculation result is displayed in Text3.
In this Code, we not only see the new code, but also see the code arrangement method. The newly entered code is reduced by four spaces, in this way, the code will appear hierarchical, which is also a good coding habit. Please follow the example and you can use "tabulation key (Tab)" for operations, when you reduce a line of code and press enter to change the line, the code will be automatically reduced.
If you want to indent a piece of code at the same time, you can first select them, and then click "set comment block" in the toolbar (instead of in the toolbox, click the uncomment block button.
6 only true and false
Don't get it wrong. I am not doing any research here. I just want to introduce a new data type, which is Boolean data.
Boolean data has only two possible values: True or False ). They are also known as yes or no.
This type of data is also frequently used. For example, in the control attributes, Locked, Visible, and Enabled are all Boolean type. For example, we can write the following code to make the text box Text1 invisible in the window:
Text1.Visible = False
In addition to these types of data, there are other types of data in VB6, which will not be described here. We will introduce them in detail later in the study.
7. UI design and code design Conversion
In the previous example, we opened the code window by double-clicking the button. In fact, on the right of the integrated development environment, that is, the attribute window aboveProject window.
For the opened code, we will find that there are two drop-down lists under the title bar, some control names on the left, and if you want to check the code for the current form, there is also a Form item in the list that refers to the current Form, while the "General" item can write some public code, and we will use it slowly in the following learning, in fact, the function or subroutine defined here is also the method of this form.
In the drop-down list on the right of the code window, some events or methods of the current control or form are listed. If you want to encode and modify an event or method, you can select here. We will introduce more events later.
So far, we have designed a simple addition computing program. If we want to enable it to perform four arithmetic operations, what should we do? Next chapter, let's start learning.