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.
 
Marshall Barton said:
With the focus restrictions that Dirk explains, there is
also the SelText proverty.

D'oh! I forgot about that!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top