dcsimg
Results 1 to 11 of 11

Thread: Autoselect text in Richtextbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    11

    Autoselect text in Richtextbox

    Hello to all !!

    I'm not new to this forum, but I've lost it for many years ...



    Now I come back with a request, if possible !!!

    Can I "automatically select" a single word in a Richtext box when I click on it?
    (I mean just one click, on the desired word, and highlight it, like when you double-click!)



    Thanks!

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    11

    Re: Autoselect text in Richtextbox

    Hi! Again!

    Nobody knows !?


    Can anyone tell me if it is possible?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    11

    Re: Autoselect text in Richtextbox

    Ah!
    I know the method to get the current word, under the mouse, but I don't know how to 'highlight' it (not in yellow, but just like when you select it in any text window)

  4. #4
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,514

    Re: Autoselect text in Richtextbox

    Try this:

    - In a project with a RichTextBox control (RichTextBox1), add a reference to the TOM library:

    Name:  2020-01-15_9-14-10.jpg
Views: 78
Size:  46.8 KB

    - Add the following code to your project:

    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
        ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long
    
    Private Const WM_USER = &H400&
    Private Const EM_GETOLEINTERFACE = (WM_USER + 60)
    
    Private Doc As tom.ITextDocument
    
    Private Sub Form_Load()
        Dim Unknown As Object
        
        SendMessage RichTextBox1.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(Unknown)  ' Got this approach for getting ITextDocument reference from Dilettante
        Set Doc = Unknown
    End Sub
    
    Private Sub RichTextBox1_Click()
       If RichTextBox1.SelLength = 0 Then
          Doc.Selection.Expand tomWord
          If Doc.Selection.End - Doc.Selection.Start > 0 Then
             Select Case AscW(Right$(Doc.Selection.Text, 1))
             Case 10, 13, 32
                ' Right-most character is whitespace, so reduce the selection by one
                Doc.Selection.MoveEnd tomCharacter, -1
             End Select
          End If
       End If
    End Sub
    - Make sure your RTB has some text typed into it, and then click any word. The clicked word should be selected.
    Last edited by jpbro; Jan 16th, 2020 at 01:50 PM. Reason: Forgot WM_ const declarations

  5. #5
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,965

    Re: Autoselect text in Richtextbox

    Code:
    Private Sub RichTextBox1_Click()
        Dim iPos1 As Long, iPos2 As Long
    
        If RichTextBox1.SelStart > 0 Then
            iPos1 = InStrRev(RichTextBox1.Text, " ", RichTextBox1.SelStart)
            iPos2 = InStr(RichTextBox1.SelStart, RichTextBox1.Text, " ")
            If iPos2 = 0 Then iPos2 = Len(RichTextBox1.Text) + 1
            If iPos1 <> iPos2 Then
                RichTextBox1.SelStart = iPos1
                RichTextBox1.SelLength = iPos2 - iPos1 - 1
            End If
        End If
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    11

    Re: Autoselect text in Richtextbox

    First of all, thanks for the reply!

    I knew this possibility (Text Object Model), but I was looking for a direct method, without adding any reference.

    However, jpbro, your code does exactly that ...

    Likewise, your Eduardo method is just what I needed.

    Personally, I had approached it, playing with positions ...
    I had found the method to go back to the word principle !!!

    Thanks to both!

  7. #7
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,514

    Re: Autoselect text in Richtextbox

    FYI Eduardo's approach has a couple of problems:

    - If you click a word at the end of a line it will also select the first word of the next line.
    - If you click a word that ends in punctuation (e.g. the next character after the word is a ".") it will select the trailing punctuation.

    If you're going to be doing any work with RichTextBoxes, I highly recommend using TOM as it has a lot of features that make working with RTB's faster and easier. Note that while it is a reference you have to add to use while developing in the IDE, there's nothing extra you have to distribute with your application.

  8. #8
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,965

    Re: Autoselect text in Richtextbox

    If you need to handle those situations the routine can be improved, I just made it for a simple case (not handling multiline), and mostly as an example.
    But if you need to extend it, and also need help doing it, no problem.

  9. #9
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,514

    Re: Autoselect text in Richtextbox

    @Eduardo - I wasn't trying to "pooh-pooh" your example, just wanted to point out some issues.

    That said, you could definitely improve your example to consider the cases I mentioned but it might quickly start to get more complicated. There is the question of "what is a written word"? In many (most?) languages each written word is separated by a space character, so that's pretty simple. Oh, except when there's punctuation like commas or periods of course. And exclamation marks...and question marks? Now that I think of it, it's not so uncommon for languages to have "unusual" quotation marks like "". Then there is those pesky bits like closing brackets, parentheses, and braces. Hmmm... this is getting fairly complicated after all!

    OK, but surely we can account for spaces and a handful of punctuation marks, right?

    Well as it turns it out, some languages don't use spaces between words. Maybe there's another character we need to add (I'm not even sure, because I don't know how those languages work). Some other languages use spaces between *syllables*, so now our space detection code might be mucking things up in certain locales.

    Looks like this is a real headache after all. Why not off-load most of that tricky stuff to Microsoft through TOM and the tomWord enumeration? TBH, I doubt they handle everything perfectly, but I can guarantee they'll do a much better job than either of us! Down the road if we encounter edge cases where MS screwed up, we can write code to account for those. Another bonus - with future OS updates, our app may even improve without requiring our intervention. Sounds like a win to me!

  10. #10
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,965

    Re: Autoselect text in Richtextbox

    Sorry, I didn't know that the OP wanted to make it multilingual and supporting all languages.
    I'm not even sure if using TOM will be the solution then.

    The OP didn't explain it in details here, and I didn't know that he was handling Vietnamese and Tibetan.
    Anyway all could be done with pure VB code if necessary, but my help here is limited to main languages.
    Last edited by Eduardo-; Jan 17th, 2020 at 12:47 AM.

  11. #11
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,514

    Re: Autoselect text in Richtextbox

    I don't know if the OP wants to do any of that either, but if you can get it (or close to it) for free then why not? Once you get into using the RTB for one feature, you'll often want to using it's other capabilities (such as font formatting, paragraph alignment, etc...) - all of which are more efficient and easier through TOM. The Vietnamese & Tibetan stuff are just examples of how seemingly simple initial requirements can eventually spiral into more complex situations. As a global company, Microsoft has probably handled some of those situations, and since we are already using their RTB control, there's really no disadvantage to use their text object model interface against it.

    As it stands right now though, your code misses out on some non-exotic situations like regular punctuation and multiple lines, so I thought it was important to at least point that out in case the OP needed to handle those cases.

    PS: With TOM there's no adding an additional library - it's built-in to the underlying rich editor DLL. If you are using the RichTextBox control already, then TOM is just 4 clicks away with no extra distribution requirements

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width