Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox text grouping

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Here's a rather interesting one. I have some prepopulated text that appears in a textbox that I placed into an excel document. I want the user to replace any text that appears within parentheses.

Example:
(This happened) and then (this happened).

Can anyone think of a way to sort of group the text together so that when the user clicks on the first (This happened) it will highlight that text so that it is easier to replace.

Please let me know if you need more information or clarification. Thanks.
 
Tricky. Not sure if this is possible as it would not be easy to tell where the cursor is located. You could tell if the cursor went into the textbox (I am assuming a Control textbox), but other than that, WHERE in the textbox.....hmmmm.

Gerry
My paintings and sculpture
 
Yep it's a Control textbox. If there is no way to do this, I would still be interested in knowing how to have a textbox select only certain text upon gaining focus. So when the user clicks on the textbox it would at least hightlight one of the groups of text that appears in ().

Thanks
 
OK, here you go.

The textbox controls has this EXACT text in it.

(This happened) and then (this happened).

The following code will select (highlight) the first "This happened" - NOT including the parenthesis - when the cursor is put anywhere in the control.
Code:
Private Sub TextBox1_GotFocus()
  TextBox1.SelStart = 1
  TextBox1.SelLength = 13
End Sub

The selection starts at 1 - the parenthesis is 0 - and ends at 13 - (This happened).

Gerry
My paintings and sculpture
 
Just to add, the only way I believe you can do this is in fact by hard-coding the location numbers. Although I suppose you could take the string contents, parse it, figure out where the locations are for a specific word, then use SelStart, and SelLength to select that word.

In fact, yes, you could do that.

Gerry
My paintings and sculpture
 
Thanks Gerry. This works well enough for my purposes I suppose.
 
In the past I have found inconsistent behaviour trying to select text on entry to a textbox. See thread707-1121247.
 
To add to fumei's code, try:
Code:
Private Sub TextBox1_GotFocus()
  TextBox1.SelStart = 1
  TextBox1.SelLength =[red] InStr(1, TextBox1.Text, ")") - 2[/red]
End Sub


Have fun.

---- Andy
 
I have to agree. Selecting can indeed be not always what one would expect.

That being said, and also stating that I feel it is good to make things efficient for the user, ummmm, I am am uncertain as to the design of this. I of course can not see the document in question, and I don't know exactly the purpose of the changing values of the textbox, but my instincts says that there is likely a better design.

Gerry
My paintings and sculpture
 
Just for completeness, here's another idea from the other thread on this topic (thread705-1350149)

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top