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 derfloh 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.
 
Hi, markronz,

Have a look at the SelStart and SelLength methods, maybe also the Len function.

HTH,

Ken S.
 
yes Eupher, absolutely, ...but most importantly the InStr()...

Dim x as integer, y as integer

x = InStr(txtMemo,"(")
y = InStr(x+1,txtMemo,")")

txtMemo.SelStart = x
txtMemo.SelLength = (y - x) + 1

I DON"T THINK YOU MEANT BRACKETS THOUGH(LOL)?

strWord = Me.txtWordSearch

x = InStr(txtMemo,strWord)

If x > 0 Then
y = Len(Me.txtWordSearch)
txtMemo.SelStart = x
txtMemo.SelLength = y
End If

 
We could have a go at this using Regular Expressions.

To demonstrate this we'll need a textbox (TextBox1) and a CommandButton (CommandButton1)

The example below will find all instances of words contained within parentheses, then loop through them and prompt the user to replace them. This is just a demonstration (and not particularly thoroughly tested) so the pattern may not be infallible and it will allow the user to enter the same word (or another word or zero length string) within parentheses but these are the more simple parts that you could change to suit your purpose.

Here's the code:
Code:
Private Sub CommandButton1_Click()
    Dim objMatches As Object
    With CreateObject("VBScript.RegExp") [green]'Create your Regular Expression Object[/green]
        .Global = True [green]'Match all occurances of the pattern[/green]
        .IgnoreCase = True [green]'will match on a non case specific basis, not strictly necessary in this example but useful to know[/green]
        .Pattern = "\(.*?\)" [green]'This pattern will (should) match any character(s) enclosed in parentheses[/green]
        
        For Each objMatches In .Execute(TextBox1.Text) [green]'loop through matches to your expression in TextBox1[/green]
            With TextBox1
                .SelStart = InStr(1, TextBox1, objMatches.Value) - 1 [green]'matches from left to right, start selection at first occurance of the matched string[/green]
                .SelLength = objMatches.Length [green]'selections length is that of the match[/green]
                TextBox1.Activate [green]'to show selection (although briefly)[/green]
                .SelText = Replace(.SelText, .SelText, InputBox("Change " & objMatches.Value & " to:")) [green]'replace selected text with value entered by user[/green]
            End With
        Next
        
    End With
    
    Set objMatches = Nothing [green]'clean up[/green]
End Sub
As I say, this isn't the finished article but it's always useful to see different ways of achieving similar results.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
If you want it to occur when the textbox gets focus (thread707-1350155) you can place the code in the TextBox1_GotFocus event and eliminate the need for the commandbutton.

Hope this helps

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