Using Bound Controls to Display Database Information


Using Bound Controls to Display Database Information

As I mentioned earlier, Visual Studio can use a variety of the controls in the Visual Studio Toolbox to display database information. You can bind controls to datasets by dragging fields from the Data Sources window (the easiest method), and you can create controls separately on your forms and bind them to dataset objects at a later time. This second option is an important feature, because occasionally you will be adding data sources to a project after the basic user interface has been created. The procedure I'll demonstrate in this section handles that situation, while giving you additional practice with binding data objects to controls within a Visual Basic application. You'll create a masked text box object on your form, configure the object to format database information in a useful way, and then bind the PhoneNumber field in StudentsDataSet to the object.

Bind a masked text box control to a dataset object

  1. Display the form in the Windows Forms Designer, and then open the Toolbox, if it is not already visible.

  2. Click the MaskedTextBox control on the Common Controls tab, and then create a masked text box object on the form below the Instructor label and text box.

    As you might recall from Chapter 6, “Using Decision Structures,” the MaskedTextBox control is similar to the TextBox control, but it gives you more ability to regulate or limit the information entered by the user into a program. The input format for the MaskedTextBox control is adjusted by setting the Mask property. In this exercise, you'll use Mask to prepare the masked text box object to display formatted phone numbers from the PhoneNumber field. (By default, phone numbers in the Students database are stored without the spacing, parentheses, or dashes of North American phone numbers, but you want to see this formatting in your program.)

  3. Click the shortcut arrow in the upper-right corner of the masked text box object, and then click the Set Mask command.

    Visual Studio displays the Input Mask dialog box, which lists a number of pre-defined formatting masks. Visual Studio uses these masks to format output in the masked text box object, as well as input received from users.

  4. Click the Phone Number input mask, and then click OK.

    The masked text box object now appears with input formatting guidelines for the country and language settings stored within Microsoft Windows. (These settings might vary from country to country, but for me it looks like a North American telephone number with area code.)

  5. Add a label object in front of the new masked text box object, and set its Text property to “Phone number:” (including the colon).

    The first descriptive label was added automatically by the Data Sources window, but we need to add this one manually.

  6. Adjust the spacing between the two labels and text boxes so that they are aligned consistently. When you're finished, your form looks similar to the following:

    graphic

    Now you'll bind the PhoneNumber field in StudentsDataSet to the new masked text box object. In Visual Studio 2005, the process is easier than it was in Visual Basic 6 or Visual Studio .NET 2003—you simply drag the PhoneNumber field from the Data Sources window onto the object that you want to bind to the data—in this case, the MaskedTextBox1 object.

  7. Display the Data Sources window if it is not visible, and then drag the PhoneNumber field onto the MaskedTextBox1 object.

    When you drag a dataset object onto an object that already exists on the form (what we might call the target object), a new bound object is not created. Instead, the DataBindings properties for the target object are set to match the dragged dataset object in the Data Sources window. (DataBindings properties are also a feature of Visual Studio .NET 2003, so this part of creating bound objects will seem familiar if you're experienced with the previous version of Visual Studio.)

    After this drag-and-drop operation, the masked text box object is bound to the PhoneNumber field, and the masked text box object's Text property contains a small database icon (a sign that the object is bound to a dataset).

  8. Verify that the MaskedTextBox1 object is selected on the form, and then press F4 to highlight the Properties window.

  9. Scroll to the DataBindings category within the Properties window, and then click the plus sign (+) to open it.

    Visual Studio displays the properties typically associated with data access in a masked text box object. Your Properties window looks similar to the following:

    graphic

    The noteworthy bound property here is the Text property, which has been set to “InstructorsBindingSource – PhoneNumber” as a result of the drag-and-drop operation. (Note that the tiny database icon does not appear here, but only in the Text property at the bottom of the alphabetical list of properties.) In addition, if you click the arrow in the Text property now, you'll see a data sources tree with the bound field highlighted. (This useful visual display allows you to quickly change the data source that the control is bound to, but don't adjust that setting now.)

  10. Click the Start Debugging button to run the program.

    Visual Studio runs the program in the IDE. After a moment, the two database fields are loaded into the text box and masked text box objects, as shown in the illustration on the next page.

    graphic

    Importantly, the masked text box object correctly formats the phone number information so that it is in the expected format for North American phone numbers.

  11. Click the Move Next button a few times.

    Another important feature is also demonstrated here: The two dataset fields scroll together, and the displayed instructor names match the corresponding phone numbers recorded in the Students database. This synchronization is handled by the InstructorsBindingNavigator object, which keeps track of the current record for each bound object on the form.

  12. Click the Close button to stop the program, and then click the Save All button to save your changes.

You've learned to display multiple database fields on a form, use the navigation toolbar to browse through a dataset, and format database information with a mask. Before you leave this chapter and move on to the useful DataGridView control discussed in Chapter 19, take a moment to see how you can further customize your dataset by using a few SQL statements.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net