VB Program Example (iii)

Source: Internet
Author: User
Tags mdb database

Create an event procedure
The code for a VB application is divided into small blocks of code called procedures. event procedures, as you are creating here, contain the code to execute when an event occurs, such as clicking a button. The control's event procedure is composed of the actual name of the control (specified in the Name property), an underscore (_), and an event name. For example, the name event procedure that is called when you click a command button named Command1 can be called a Command1_Click event procedure.
To create an event procedure, follow these steps:
1. In the Object list box, select an object name in the active form (the active form refers to the form that currently has focus). In this example, the command button Command1 is selected.
2. In the Procedure list box, select the event name of the specified object. The Click procedure is already selected because it is the default procedure for a command button. Note that the template for the event procedure is already displayed in the Code window.
3. Enter the following code between the sub and End Sub statements:
Text1.Text = "Hello, world!"
This event process should be:
Private Sub Command1_Click ()
Text1.Text = "Hello, world!"
End Sub
It's worth noting that the code here simply changes the Text property of the control Text1 and reads "Hello, world!". The syntax of this example takes object. The format of the property, where Text1 is an object and Text is an attribute. You can use this syntax to change the property setting values of any form or control when responding to events that occur in the application's run.
For more information about creating other types of procedures, see the "Process Overview" section in Chapter fifth, "Programming basics."

Running the application
In order to run the application, you can choose Start from the Run menu, or click the Start button in the toolbar, or press the F5 key. Click the command button that you just created on the form, and the text box will display "Hello, world!."

Firstapp sample Applications
Since VB provides a much richer tool than this example application, many of its features are immediately available for managing and compounding applications. Reviewing the sample application is an excellent way to learn VB. The following example shows how easy it is to compile a practical application using VB. The
Application Firstapp demonstrates how to use the data control and the grid control to display a table of information taken from a database. VB makes access to the database from within the application very easy. Data controls provide the ability to roam through a recordset in a database and to synchronize the records that are displayed in the grid control with the location of the data set.
This example contains a data control, a MSFlexGrid control, a list box control, and two command buttons. The grid displays the information table for the products retrieved from the Northwind database. When you select an item in the grid by using the Anchor button on the data control, the selected product name is displayed on the data control. You can also double-click the current selection in the grid to add the currently selected title in the list box control's purchase order.
Use the AddItem method to add information for a list box. Method is a VB function that acts on a specific object (in this case, a ListBox object). The syntax (object.method) of the specified method is similar to the syntax for setting the property (Object.property). The AddItem method can dynamically add the contents of a list box while the program is running. Instead, the clear method clears all the information for the list box.
For more information about methods, see the section "Properties, Methods, and Events Overview" in Chapter Three, "Forms, controls, and menus."

Create a project
To create a project first, select New Project from the File menu, and then select Standard EXE from the New Project dialog box (the New Project dialog box will appear the first time you start VB). VB creates a new project and displays a new form. The application's interface is drawn with a data control, a MSFlexGrid control, a list box control, and two command buttons. The MSFlexGrid control is not in the default tool box, so you must add:
To add a control to the toolbox, follow these steps:
1. "Parts" In the context menu of the selected toolbox (you can right-click inside the Toolbox window to display the context menu.) )。 The Parts dialog box is displayed.
2. Locate the MSFlexGrid (Microsoft Flex Grid 6.0) item in the Controls list box, and then select the check box to the left of the control.
3. Click "OK" button. There is an icon for the MSFlexGrid control in the Toolbox.
Use the toolbox to draw a data control, a MSFlexGrid control, a list box control, and two command buttons on a form. If you don't remember how to draw, see "Creatingthe Interface" earlier in this chapter.
Set properties The properties of the object are set in the Properties window according to the following table, and the remaining properties use the default values.

The DatabaseName property of the data control must contain the actual path of the database. By default, the Biblio.mdb Database button is installed in the same directory as VB. When you select the DatabaseName property in the Properties window, you can click the button to the right of the property to display the standard Open File dialog box to browse the file. When you finish setting the DatabaseName property, the RecordSource property in the Properties window will contain a list of the tables or recordsets in the selected database. Setting the DataSource property of the DataGrid control to Data1 automatically links the grid and data control.

The

Write event code
Program contains all of the code in the Command1_Click, Command2_Click, data1_reposition, and Datagrid1_dblclick event procedures. Double-click the form or control to display the Code window, and then enter the code for each event procedure.
to add the following code to the Command1_Click event procedure to clear the list box when the button is clicked:
Private Sub Command1_Click ()
List1.clear ' Clears the list box.
End Sub
The above statement calls the clear method of the list box List1, and the Clear method deletes the contents of the list box.
Add the following code to the Command2_Click event procedure to unload the form in memory and terminate the application:
Private Sub command2_click ()
Unload Form1
End to terminate the application The first statement of the procedure above
end Sub
invokes the Unload event of the form. If you need to perform a function (such as saving a file) when you terminate the program, you can place the code in the Unload event procedure for the form. The second statement calls the End function to terminate the program.
Add the following code to the Data1_reposition event procedure to update the caption whenever a record is selected:
Private Sub data1_reposition ()
Data1.caption = Data1.recordset ("title")
End Sub
The preceding statement assigns the right value (the Title field of a data control's Recordset) to the property on the left (the Caption property of the Data Control object). The
adds the following code to the Datagrid_dblclick event procedure, which is used to add information to a list box when you double-click a selected row.
Private Sub Datagrid1_dblclick ()
List1.AddItem data1.recordset ("Title")
End Sub
calls the list box List1 AddItem method. The text added to the list box is included in the parameter of the method (this is the title field of the recordset for the Data Control). Passing a value to an argument is similar to assigning a value to a property, unlike an assignment statement that does not require an equal sign.

Preservation Project
Select the Save Project command from the File menu to end this creation of the application. VB will be prompted to save the form and save the project respectively. You can name the project "Bookstore Sales List" ("Bookstore Shopping list."). Windows 95 and Windows NT both allow filenames with up to 255 characters and can contain spaces. The old version of MicrosoftWindows allows only eight-character filenames plus a three-character extension.

Enhancing applications
So the first VB application is complete: This is a simple but useful program. You can use this as a basis to add new functionality to your application and replace Biblio.mdb with your own data. Of course, to make this program really useful, you also need to add basic features to save or Print list box content, add more information such as price, availability, or even collect credit card information and send orders over the Internet. By continuing to read the remainder of the programmer's Guide, you will find all the examples and other knowledge about these features.

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.