SelectText

Selects text in an editable control.

To select text in

Use

Any editable control, other than a RichTextEdit

Syntax 1 For editable controls (except RichTextEdit)

A RichTextEdit control or a DataWindow whose object has the RichTextEdit presentation style

Syntax 2 For RichTextEdit controls and presentation styles


Syntax 1 For editable controls (except RichTextEdit)

Description

Selects text in an editable control. You specify where the selection begins and how many characters to select.

Applies to

DataWindow, EditMask, InkEdit, MultiLineEdit, SingleLineEdit, DropDownListBox, and DropDownPictureListBox controls

Syntax

editname.SelectText ( start, length )

Argument

Description

editname

The name of the DataWindow, EditMask, InkEdit, MultiLineEdit, SingleLineEdit, DropDownListBox, or DropDownPictureListBox control in which you want to select text.

start

A long specifying the position at which you want to start the selection.

length

A long specifying the number of characters you want to select. If length is 0, no text is selected but PowerBuilder moves the insertion point to the location specified in start.

Returns

Integer for DataWindow and list boxes, Long for other controls.

Returns the number of characters selected. If an error occurs, SelectText returns -1.

Usage

If the control does not have the focus when you call SelectText, then the text is not highlighted until the control has focus. To set focus on the control so that the selected text is highlighted, call the SetFocus function.

NoteHow much to select When you want to select all the text of a line edit or select the contents from a specified position to the end of the edit, use the Len function to obtain the length of the control’s text.

To select text in a DataWindow with the RichTextEdit presentation style, use Syntax 2.

Examples

Example 1

This statement sets the insertion point at the end of the text in the SingleLineEdit sle_name:

sle_name.SelectText(Len(sle_name.Text), 0)

Example 2

This statement selects the entire contents of the SingleLineEdit sle_name:

sle_name.SelectText(1, Len(sle_name.Text))

Example 3

The rest of these examples assume the MultiLineEdit mle_EmpAddress contains Boston Street.

The following statement selects the string ost and returns 3:

mle_EmpAddress.SelectText(2, 3)

Example 4

The next statement selects the string oston Street and returns 12:

mle_EmpAddress.SelectText(2, &

		Len(mle_EmpAddress.Text))

Example 5

These statements select the string Bos, returns 3, and sets the focus to mle_EmpAddress so that Bos is highlighted:

mle_EmpAddress.SelectText(1, 3)

mle_EmpAddress.SetFocus()

See also


Syntax 2 For RichTextEdit controls and presentation styles

Description

Selects text beginning and ending at a line and character position in a RichTextEdit control.

Applies to

RichTextEdit and DataWindow controls

Syntax

rtename.SelectText ( fromline, fromchar, toline, tochar { band } )

Argument

Description

rtename

The name of the RichTextEdit or DataWindow control in which you want to select text. The DataWindow object in the DataWindow control must be a RichTextEdit DataWindow.

fromline

A long specifying the line number where the selection starts.

fromchar

A long specifying the number in the line of the first character in the selection.

toline

A long specifying the line number where the selection ends. To specify an insertion point, set toline and tochar to 0.

tochar

A long specifying the number in the line of the character before which the selection ends.

band (optional)

A value of the Band enumerated datatype specifying the band in which to make the selection. Values are:

  • Detail!

  • Header!

  • Footer!

The default is the band that contains the insertion point.

Returns

Long. Returns the number of characters selected. A carriage return with a line feed counts as a single character. If an error occurs SelectText returns -1. If any argument’s value is null, it returns null.

Usage

The insertion point is at the "to" end of the selection, that is, the position specified by toline and tochar. If toline and tochar are before fromline and fromchar, then the insertion point is at the beginning of the selection.

You cannot specify 0 for a character position when making a selection.

You cannot always use the values returned by Position to make a selection. Position can return a character position of 0 when the insertion point is at the beginning of a line.

To select an entire line, set the insertion point and call SelectTextLine. To select the rest of a line, set the insertion point and call SelectText with a character position greater than the line length.

Examples

Example 6

This statement selects text from the first character in the RichTextEdit control to the fourth character on the third line:

rte_1.SelectText(1,1, 3,4)

Example 7

This statement sets the insertion point at the beginning of line 2:

rte_1.SelectText(2,1, 0,0)

Example 8

This example sets the insertion point at the end of line 2 by specifying a large number of characters. The selection highlight extends past the end of the line:

rte_1.SelectText(2,999, 0,0)

Example 9

This example sets the insertion point at the end of line 2 by finding out how long the line really is. The code moves the insertion point to the beginning of the line, gets the length, and then sets the insertion point at the end:

long ll_length


//Make line 2 the current line

rte_1.SelectText(2,1, 0,0)


// Specify a position after the last character

ll_length = rte_1.LineLength() + 1


// Set the insertion point at the end

rte_1.SelectText(2,ll_length, 0,0)

rte_1.SetFocus()

Example 10

This example selects the text from the insertion point to the end of the current line. If the current line is the last line, the reported line length is 1 greater than the number of character you can select, so the code adjusts for it:

long ll_insertline, ll_insertchar

long ll_line, ll_count


// Get the insertion point

rte_1.Position(ll_insertline, ll_insertchar)


// Get the line number and line length

ll_line = rte_1.SelectedLine()

ll_count = rte_1.LineLength()

// Line length includes the eof file character,

// which can't be selected

IF ll_line = rte_1.LineCount() THEN ll_count -= 1


// Select from the insertion point to the end of

// line

rte_1.SelectText(ll_insertline, ll_insertchar, &

ll_line, ll_count)

See also