Selected text of TextBox -> string

  • Thread starter Thread starter Guest
  • Start date Start date
Henry said:
how can I optain the marked text within a TextBox ?

While the text is selected -- which means while the text box has the
focus -- the control's .SelStart and .SelLength properties tell where
the selection begins and how long it is, so you can use them to extract
the selected substring from the .Text property:

With Me.txtMyTextbox
If .SelLength > 0 Then
strSelected = Mid$(.Text, .SelStart + 1, .SelLength)
Else
strSelected = vbNullString
End If
End With

However, when the control loses the focus, the selection will be lost.
So if you want to have this information available in some other event,
you may need to capture it in the control's Exit event, before the focus
is lost, and store it in a module-level variable.
 
Henry said:
how can I optain the marked text within a TextBox ?


With the focus restrictions that Dirk explains, there is
also the SelText proverty.
 
Back
Top