Allowing Users to Enter Text Using a Text Box

   

A Label control is usually the best control for displaying text a user can't change. However, when you need to let users enter or edit text, the text box is the tool for the job. If you've ever typed information on a form, you've almost certainly used a text box. Add a new text box to your form now by double-clicking the TextBox item in the toolbox. Set the text box's properties as follows :

Property Value
Name txtMyTextBox
Location 128,4
Size 136,20

When you first create a new text box, its Text property is set to its default name (see Figure 7.2). Unfortunately, the Text property isn't automatically changed when you change the name of the text box (which you should always do), so I recommend that you clear out the Text property of new text boxes. Delete the text in the Text property now and notice that the text box appears empty on the form.

Figure 7.2. A new text box has its Text property set to its default name.

graphics/07fig02.jpg


Although you'll probably want to clear the Text property of most of your text boxes at design time, understanding certain aspects of the text box is easier when a text box contains text. Set the text box's Text property to This is sample text. Remember to press Enter or Tab to commit your property change.

Specifying Text Alignment

Both the TextBox and the Label controls have a TextAlign property (as do many other controls). The TextAlign property determines the alignment of the text within the control ”very much like the justification setting in a word processor. You can select from Left, Center, or Right.

graphics/bulb.gif

Label controls allow you to set the vertical alignment and the horizontal alignment using the TextAlign property. This works best when AutoSize is set to false.

Change the TextAlign property of the text box to Right, and see how the text becomes right-aligned within the text box. Next, change TextAlign to Center to see what center alignment looks like. As you can see, this property is pretty straightforward. Change the TextAlign property back to Left before continuing.

Creating a Multiline Text Box

In the previous hour , I talked about the sizing handles of a selected control. I mentioned how handles that can be sized appear filled with white, and handles that are locked appear with a gray center. Notice how only the left and right edges of the text box have white sizing handles. This means that you can adjust only the left and right edges of the control (you can alter only the width, not the height). This is because the text box is defined as a single-line text box, meaning it will display only one line of text. What would be the point of a really tall text box that showed only a single line?

To allow a text box to display multiple lines of text, set its Multiline property to true. Set the Multiline property of your text box to true now, and notice that all the sizing handles become white.

Change the Text property of the text box to This is sample text. A multiline text box will wrap its contents as necessary. Press Enter or Tab to commit the property change. Notice how the text box displays only part of what you entered because the control simply isn't big enough to show all the text (see Figure 7.3). Change the Size property to 136,60, and you'll then see the entire content of the text box (see Figure 7.4).

Figure 7.3. A text box may contain more text than it can display.

graphics/07fig03.jpg


Figure 7.4. A multiline text box can be sized as large as necessary.

graphics/07fig04.jpg


At times, you won't want a user to be able to interact with a control. For instance, you may implement a security model in your application, and if the user doesn't have the necessary privileges, you may not want the user to be able to alter data. The Enabled property, which almost every control has, determines whether the user can interact with the control. Change the Enabled property of the text box to false, and press F5 to run the project. Although no noticeable change occurs in the control in Design view, there is a big change to the control at runtime: the text appears in gray rather than black, and the text box won't accept the focus or allow you to change the text (see Figure 7.5).

Figure 7.5. You can't interact with a text box whose Enabled property is set to false.

graphics/07fig05.jpg


Stop the project now by choosing Stop Debugging from the Debug menu, and then change the control's Enabled property back to true.

Adding Scrollbars

Even though you can size a multiline text box, there may be times when the contents of the control are more than what can be displayed. If you believe that this is a possibility for a text box you're adding to a form, give the text box scrollbars by changing the ScrollBars property from None to Vertical, Horizontal, or Both.

graphics/bookpencil.gif

For a text box to display scrollbars, its Multiline property must be set to true.

Change the ScrollBars property of your text box to Vertical and notice how scrollbars appear in the text box (see Figure 7.6).

Figure 7.6. If a text box may contain lots of text, give it a scrollbar.

graphics/07fig06.jpg


Limiting the Number of Characters a User Can Enter

You can limit the number of characters a user can type into a text box using the MaxLength property. All new text boxes are given the default value of 32767 for MaxLength, but you can change this as needed (up or down). Add a new text box to the form and set its properties as follows:

Property Value
Name txtRestrict
Location 128,80
MaxLength 10
Size 136,20
Text (make blank)

Run the project by pressing F5 and then enter the following text into the text box: So you run and you run. Be sure to try to enter more than 10 characters of text ”you can't (if you can, you're probably entering the text into the text box with scrollbars, rather than into the new text box). All that you're allowed to enter is So you run (10 characters). The text box allows only 10 characters, whether that's via entry using the keyboard or a Paste operation. The MaxLength property is most often used when the text box's content is to be written to a database, in which field sizes are usually restricted (using a database is discussed in Hour 21, "Working with a Database").

Stop the project by choosing Stop Debugging from the Debug menu and then click Save All on the toolbar.

Creating Password Fields

You've probably used a password field: a text box that displays an asterisk for each character entered. Any text box can be made a password field by assigning a character to its PasswordChar field. Select the PasswordChar property of the second text box now (txtRestrict) and enter an asterisk ( * ) for the property value. Run the project once more and enter text into the text box. Now an asterisk is displayed for each character you enter (see Figure 7.7). Although the user doesn't see the actual text contained in the text box, referencing the Text property in code always returns the true text.

Figure 7.7. A password field displays its password character for all entered text.

graphics/07fig07.jpg


graphics/bookpencil.gif

A text box will display password characters only if its Multiline property is set to false.

Stop the project by choosing Stop Debugging from the Debug menu. Delete the asterisk from the PasswordChar field, and then save the project by clicking Save All on the toolbar.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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