|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Creating A Password ControlIts time to heighten security. Users of your new SuperSpecialDataBase program are worried about the low security of your program, so you decide to add a little security with password controls. Visual Basic will help out. To convert a standard text box into a password box, you just assign some character (usually an asterisk [*]) to the text boxs PasswordChar property. After that, your program can read the text in the text box, but only the password character will appear on the screen each time the user types a character, as shown in Figure 6.7.
Controlling Input In A Text BoxThe Testing Department is on the phonetheres a bug in your program. The users are getting runtime errors. Dont panic, you say; youll be right down. You ask the users to duplicate what caused the problem, and you find that theyre trying to add two numbers with your program: 15553 and 955Z. Whats 955Z? you ask. A typo, they say. Is there any way you can restrict user input so this doesnt happen? Yes, you can. Just use the KeyPress event and check the KeyAscii parameter, which is the ANSI (not ASCII, despite its name) code for the just-struck key. Lets make this clearer with an example; heres how you would restrict users to only typing digits into Text1; all non-digits are simply discarded:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
Besides the KeyPress, text boxes support the KeyUp and KeyDown events, although the KeyPress event is easiest to use, because you get the character code of the typed character passed to you immediately. In the KeyUp and KeyDown events, you are passed a virtual key code you have to translate into a character, after checking to see if the Shift key was down and so on. You can also use the text boxs Change event, which occurs when theres a change in the text boxs text. Adding An RTF Box To A FormSo youve decided to make the move from text boxes to rich text boxes, and you turn to the toolbox. Wait a minutewheres the Rich Text Box tool in the toolbox? The answer is that its not there until you add it. To add a rich text box to a form, follow these steps:
Accessing Text In A Rich Text BoxTo access text in a rich text box, you can use two properties: Text and TextRTF. As their names imply, Text holds the text in a rich text box in plain text format (like a text box), and TextRTF holds the text in Rich Text Format. Heres an example where we read the text in RichTextBox1 without any RTF codes and display that text as plain text in RichTextBox2:
Private Sub Command1_Click()
RichTextBox2.Text = RichTextBox1.Text
End Sub
Heres the same operation where we transfer the text including all RTF codesthat is, here were transferring rich text from one rich text box to another:
Private Sub Command1_Click()
RichTextBox2.TextRTF = RichTextBox1.TextRTF
End Sub
Selecting Text In Rich Text BoxesRich text boxes support the SetText property just like standard text boxes. However, SetText only works with plain text. You can set the start and end of plain-text selection with the SelStart and SelLength properties. If you want to work with RTF-selected text, on the other hand, use the SelRTF property. For example, heres how we select the first 10 characters in RichTextBox1 and transfer them to RichTextBox2 using SelRTF:
Private Sub Command1_Click()
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 10
RichTextBox2.TextRTF = RichTextBox1.SelRTF
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |