Capturing selected text in a text box

  • Thread starter Thread starter Robin S.
  • Start date Start date
R

Robin S.

I want to put HTML bold (<b></b>) tags around certain words and
headings in a text box in an Access form. I thought a good way to make
this process faster would be to use a command button which would take
selected text within a text box and place the tags at the beginning
and end of the string.

Apparently the .SelText and smiliar properties are only available if a
control has focus. The problem is that setting focus (.Setfocus) to a
textbox which as some text selected ends up selecting ALL the text in
the control! Doh!

Anyone know how to do this?

Thanks for any help and suggestions!

Regards,

Robin
 
You can use the SelStart and SelLength properties to control what's selected
in the text box once it has focus.
 
You can use the SelStart and SelLength properties to control what's selected
in the text box once it has focus.

Doug,

I should have been more clear. I want to select (highlight with the
mouse) the text to have the tags, then click on a command button and
have the selected text be updated. When the command button is clicked,
it has focus. If I use .SetFocus on the text box, the entire text of
the textbox becomes selected which isn't useful.

Perhaps I just don't know how to set focus back to the textbox
correctly.

Regards,

Robin
 
Robin said:
I want to put HTML bold (<b></b>) tags around certain words and
headings in a text box in an Access form. I thought a good way to make
this process faster would be to use a command button which would take
selected text within a text box and place the tags at the beginning
and end of the string.

Apparently the .SelText and smiliar properties are only available if a
control has focus. The problem is that setting focus (.Setfocus) to a
textbox which as some text selected ends up selecting ALL the text in
the control!


How do you identify the text to changed?

How do you indicate what change is to made to the text?

If you want users to select the text they want to change by
dragging across it in the text box and then clicking a
button to effect a specific change, then you need to save
the position and length of the selected text in the text
box's Exit event. Then the button can set the focus back to
the text box and set the SelStart and SelLength properties.

Here's some air code with the general idea:

module level declarations:
Private lngStart As Long, lngLength As Long

text box exit event:
lngStart = Me.thetextbox.SelStart
lngLength = Me.thetextbox.SelLength

bold button's click event:
Me.thetextbox.SetFocus
Me.thetextbox.SelStart = lngStart
Me.thetextbox.SelLength = lngLength
Me.thetextbox.SelText = "<b>" & Me.thetextbox.SelText &
"</b>"
 
How do you identify the text to changed?

How do you indicate what change is to made to the text?

If you want users to select the text they want to change by
dragging across it in the text box and then clicking a
button to effect a specific change, then you need to save
the position and length of the selected text in the text
box's Exit event.  Then the button can set the focus back to
the text box and set the SelStart and SelLength properties.

Here's some air code with the general idea:

module level declarations:
Private lngStart As Long, lngLength As Long

text box exit event:
        lngStart = Me.thetextbox.SelStart
        lngLength = Me.thetextbox.SelLength

bold button's click event:
        Me.thetextbox.SetFocus
        Me.thetextbox.SelStart = lngStart
        Me.thetextbox.SelLength = lngLength
        Me.thetextbox.SelText = "<b>" & Me.thetextbox.SelText &
"</b>"

Marsh,

This is exactly what I was looking for! The exit event!

Thanks very much guys. You've been a wonderful help.

Regards,

Robin
 
Back
Top