Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
-
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
-
Error Handling (2)
-
VB6 Database


You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Using TextBox Control In Visual Basic 6

TextBox controls offer a natural way for users to enter a value in your program. For this reason, they tend to be the most frequently used controls in the majority of Windows applications. TextBox controls, which have a great many properties and events, are also among the most complex intrinsic controls. In this section, I guide you through the most useful properties of TextBox controls and show how to solve some of the problems that you're likely to encounter.

Setting properties to a TextBox

  • Text can be entered into the text box by assigning the necessary string to the text property of the control

  • If the user needs to display multiple lines of text in a TextBox, set the MultiLine property to True

  • To customize the scroll bar combination on a TextBox, set the ScrollBars property.

  • Scroll bars will always appear on the TextBox when it's MultiLine property is set to True and its ScrollBars property is set to anything except None(0)

  • If you set the MultilIne property to True, you can set the alignment using the Alignment property. The test is left-justified by default. If the MultiLine property is et to False, then setting the Alignment property has no effect.

Run-Time Properties of a TextBox control

The Text property is the one you'll reference most often in code, and conveniently it's the default property for the TextBox control. Three other frequently used properties are these:

  • The SelStart property sets or returns the position of the blinking caret (the insertion point where the text you type appears). Note that the blinking cursor inside TextBox and other controls is named caret, to distinguish it from the cursor (which is implicitly the mouse cursor). When the caret is at the beginning of the contents of the TextBox control, SelStart returns 0; when it's at the end of the string typed by the user, SelStart returns the value Len(Text). You can modify the SelStart property to programmatically move the caret.

  • The SelLength property returns the number of characters in the portion of text that has been highlighted by the user, or it returns 0 if there's no highlighted text. You can assign a nonzero value to this property to programmatically select text from code. Interestingly, you can assign to this property a value larger than the current text's length without raising a run-time error.

  • The SelText property sets or returns the portion of the text that's currently selected, or it returns an empty string if no text is highlighted. Use it to directly retrieve the highlighted text without having to query Text, SelStart, and SelLength properties. What's even more interesting is that you can assign a new value to this property, thus replacing the current selection with your own. If no text is currently selected, your string is simply inserted at the current caret position.

When you want to append text to a TextBox control, you should use the following code (instead of using the concatenation operator) to reduce flickering and improve performance:

Text1.SelStart = Len(Text1.Text)
Text1.SelText = StringToBeAdded

One of the typical operations you could find yourself performing with these properties is selecting the entire contents of a TextBox control. You often do it when the caret enters the field so that the user can quickly override the existing value with a new one, or start editing it by pressing any arrow key:

Private Sub Text1_GotFocus()
Text1.SelStart = 0
' A very high value always does the trick.
Text1.SelLength = 9999
End Sub

Always set the SelStart property first and then the SelLength or SelText properties. When you assign a new value to the SelStart property, the other two are automatically reset to 0 and an empty string respectively, thus overriding your previous settings.

The selected text can be copied to the Clipboard by using SelText:

Clipboard.SelText text, [format]

In the above syntax, text is the text that has to be placed into the Clipboard, and format has three possible values.

1. VbCFLink - conversation information
2. VbCFRTF - Rich Text Format
3. VbCFText - Text

We can get text from the clipboard using the GetText() function this way:

Clipboard.GetText ([format])

The following Figure summarizes the common TextBox control's properties and methods.

Property/ Method
Description

 

Properties

Enabled specifies whether user can interact with this control or not
Index Specifies the control array index
Locked If this control is set to True user can use it else if this control is set to false the control cannot be used
MaxLength Specifies the maximum number of characters to be input. Default value is set to 0 that means user can input any number of characters
MousePointer Using this we can set the shape of the mouse pointer when over a TextBox
Multiline By setting this property to True user can have more than one line in the TextBox
PasswordChar This is to specify mask character to be displayed in the TextBox

ScrollBars

 

This to set either the vertical scrollbars or horizontal scrollbars to make appear in the TextBox. User can also set it to both vertical and horizontal. This property is used with the Multiline property.
Text Specifies the text to be displayed in the TextBox at runtime
ToolTipIndex This is used to display what text is displayed or in the control
Visible By setting this user can make the Textbox control visible or invisible at runtime

 

Method

 
SetFocus Transfers focus to the TextBox

 

Event procedures

 
Change Action happens when the TextBox changes
Click Action happens when the TextBox is clicked
GotFocus Action happens when the TextBox receives the active focus
LostFocus Action happens when the TextBox loses it focus
KeyDown Called when a key is pressed while the TextBox has the focus
KeyUp Called when a key is released while the TextBox has the focus

More on VB6 TextBox Controls

See Also


  

Previous Page | Table of Contents | Next Page

   

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved