Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

458 Part IV Database and Web Programming


of objects from your databases when you build datasets using the Data Source Configuration
Wizard. As you can see, it is not necessary to create bound objects for each dataset item on
a form—you can decide which database records you want to use and display.

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 Business Phone field
in Faculty2010DataSet 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 Last Name 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 Business Phone field. (By default, phone numbers in the Faculty2010 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 smart tag 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 Windows. (These settings might vary from
Free download pdf