SelStart and SelLength VB Help

The SelStaH property returns or sets the position of the first character of the selected text, somewhat like placing the cursorat a specific location in the text and selecting text by dragging the mouse. The SelLmgth property returns or sets the length of the selected text. The most common use of these two propertiesis to extract the user’s selection or to select a piece of text from within the application. One’ example is to extract the location of a string in the text.
The Form shown in Figure 5.2 contains a textbox and two labels. Each time the .user selects some text with the mouse.the following lines are executed from within  the control’s MouseUp event, and they display the selected text’s starting location and length in the ~o ‘labels:

labell.Caption     – ‘SelStart –    • & Textl.SelStart
labe12.Caption   – ‘Sellength – • &Textl. Sel length

This example reads the values of the SelStart and SeILength properties. If you . assign values to them! you can select text from within your’ application.

Suppose the user is seeking the wordfVisual” in the control’s text. The function will locate the string, but it won’t select it. The found string may even be outside the visible area of the control. You can add a few more lines of code to select the word in the text and highlight it so that the user will spot it instantly These lines locate the string “Visual” (or any user-supplied string stored in the seekString variable) in the text and select it by setting the SeIStart and SeILength properties, Moreover, if the string is outside the visible area of the control, the text can be scrolled with the appropriate scroll bar, so that the selected text becomes  visible.
As far as the appearance of the selected text goes, it doesn’t make any difference whether it was selected by the user or by the application; it appears in reverse colon as is common with all text editors

The few lines of code shown above form the core of a text editor’s Search command. Replacing the current selection with another string is as simple as assigning a new value to the SelText property and-it provides you with an easy implementa-‘ tion of a Search & Replace ratio. Designing a Form to include the control on ; which the Search & Replace strings’will be entered and the Command buttons will take more effort than implementing the Search & Replace logic

The SeIStart and SelLength properties always have a value even if no text has been selected. In this case, SelJ:ength is 0, and SelStart is the current location of the pointer in the text. If you want to insert some text at the pointer’s location, simply asSign it to the SelText property.

The selected text will remain highlighted even when the user moves to another control or Form. If you want to change the appearance of the selected text, use the HideSelection property. Its default value is False, which is why the text remains highlighted even when the textbox loses the focus. If you set the Hide5election property to True, the selected text will be highlighted only as long as the TextBox control has the focus.

VB6 at Work: The TextPad Projed

The TextPad application, shown in Figure 5.3, demonstrates mostof the TextBox control’s properties and methods described so far. TextPad is a basic text editor that you can incorporate in your programs and customize for special applications. The TextPad’s ~rm is covered by a TextBoxcontrol. Every time the user changes the size of the Form the application adjusts the size of the TextBox control accordingly.

The menu bar of the Form contains all the commands you’d expect to find in text-editing applications:


New      Clears the text
Open    Loads a new text file from disk
Save     Saves the text to :itsfile on disk

SillVeAs   Saves the text with a new filename on disk
Exit          Terminates the application


Copy       Copies selected text to the Clipboard
Cut Cuts  selected text
Paste      Pastes the Clipboard’s contents to the text
Select All Selects all the text in the control
Find        Displays a dialogbox with Find & Replace options


Upper Case    Converts selected text to uppercase
LOwer Case    Converts selected text to lowercase
Number Unes   Numbers the text lines


Font        Sets the text’s font, size, and attributes
Page      Color Sets the control’s background color
Text       Color Sets the color of the text

Design this menu Using the techniques explained in Chapter 4, Working withForms. The File menu options are implemented with the Common  Dialogs control, which  discussed shortly, As you’ll see, you don’t have to design the File Open and File~ boxes. All you have to do i,splace another control on the Form and set a few properties;’ takes it from there. The application will display the standard File Open and File log boxes in which the user can select or specify a filename; the File Open common dailog control then reports this filenaine to the application.

The options on the Edit menu move selected text to and from the Clipboard, For the TextPad application. all you need to know about the Oipboard are the SetText method, w¥.ch places a string on the Clipboard, and the GetText method, which retrieves text from the Clipboard and, copies it to a string variable (see Figure 5.4). The Copy command, ‘for example, is implemented with a singlejine of code (Editor is the name of the TextBox control):

The Cut command does the same, but it also clears the selected text:

If no text is currently selected, the Clipboard’s text is pasted at the pointer’s current location. If the Clipboard contains a bitmap (placed there by another application), then the paste operation will fail. You can insert the statement:

 On Error Resume Next

to prevent the application from crashing, sh uld file Clipboard contain anything else than text.

The best method to handle the Paste operation is to use the GetFormat method of the Clipboard object, which lets you examine the contents of the Clipboard and act accordingly. The GetFo~t method doesn’t return the format of the data

stored in the Clipboard. It lets you examine whether the Clipboard contains data of a ‘specific format. To find out whether the Clipboard contains text, use the following If structure

Posted on November 4, 2015 in Basic ActiveX Controls

Share the Story

Back to Top
Share This