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
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
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 information2. 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 |
ScroolBars | 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 |
0 comments:
Post a Comment