Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating A Password Control

It’s 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 box’s 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.


Figure 6.7  Creating a password control.


TIP:  You may be concerned that someone can copy the text in a password control and paste it into a word processor to read it, but in fact, Clipboard-handling from the text box is disabled if you are using a password character.


WARNING!  A note about security: don’t trust the password control too far, because there may be some security loopholes in it that someone out there can exploit. I once wrote an article that included a tiny program to encrypt data in a minimum-security way just to get readers started and got a letter full of angry satisfaction from a code-breaking expert who told me it had taken him “only” five days (with full-time access to a supercomputer) to break a file encoded with my program.

Controlling Input In A Text Box

The Testing Department is on the phone—there’s a bug in your program. The users are getting runtime errors. Don’t panic, you say; you’ll be right down.

You ask the users to duplicate what caused the problem, and you find that they’re trying to add two numbers with your program: 15553 and 955Z. What’s 955Z? you ask. A typo, they say. Is there any way you can restrict user input so this doesn’t 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. Let’s make this clearer with an example; here’s 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 box’s Change event, which occurs when there’s a change in the text box’s text.

Adding An RTF Box To A Form

So you’ve decided to make the move from text boxes to rich text boxes, and you turn to the toolbox. Wait a minute—where’s the Rich Text Box tool in the toolbox? The answer is that it’s not there until you add it.

To add a rich text box to a form, follow these steps:

1.  Select the Project|Components menu item.
2.  Click the Controls tab in the Components box.
3.  Find and select the Microsoft Rich Textbox Control box, and click on OK to close the Components box.
4.  The rich text control now appears in the toolbox (at lower right in Figure 6.1), and you can use it to add rich text boxes to your forms, as shown in Figure 6.8.


Figure 6.8  Displaying rich text in a rich text box.


TIP:  What these steps really accomplish is to add the Richtx32.ocx file to your program, and you’ll need to distribute that file with your program if you use rich text boxes.

Accessing Text In A Rich Text Box

To 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.

Here’s 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

Here’s the same operation where we transfer the text including all RTF codes—that is, here we’re 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 Boxes

Rich 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, here’s 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


Previous Table of Contents Next


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.