Results 1 to 17 of 17

Thread: Finding locations within Richtextbox

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Question

    I need to be able to click or doubleclick anywhere in the body of a richtext box and have the value of a string I be inserted into that part of the text file in the richtextbox overwriting what was already there.

    For example the first line of the RTFBox is

    The cat sat on the mat

    I have "dog" in memory as a string variable somewhere when I click just before the c of cat the sentance comes out like this

    The dog sat on the mat

    and not....

    The dogcat sat on the mat.

    If you know what I mean.

    Anyone any ideas ? Cheers.

    DQ

  2. #2
    Guest
    Try this.
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        sIn.SetFocus
        For I = 1 To Len(sWord)
            sIn.SelText = Mid(sWord, I, 1)
            SendKeys "{DEL}"
        Next I
    End Function
    You would use it like:
    Code:
    InsertWord RichTextBox1, "dog"

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Or you could do this:
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        sIn.SelText = Mid(sWord, I, 1)
    End Function
    Good luck!

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39
    Thanks guys,
    Unfortunately neither of those work for me, Megatron when I put your code in, and then call the function in the on click event of the richtextbox you briefly see "dog" appear displacing the text before it then deletes itself as well as the first digit to the right of where you clicked ie:

    The cat sat on the mat ......becomes
    The at sat on the mat instead of the dog sat on the mat.

    Joacim, firstly I got i not declared as a variable (this project uses Option Explicit), then when I did declare it in the function as an integer I get a run time error 5 "Invalid procedure or argument" on the last line of code.

    I am sure that I must be doing something wrong.

    DQ

  5. #5
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    This works:

    Code:
    Private Sub RichTextBox1_Click()
     Dim MyString As String, TempString As String
    '********************************************
     MyString = "dog"
     With RichTextBox1
      TempString = .Text
      Mid(TempString, .SelStart + 1, Len(MyString)) = MyString
      .Text = TempString
     End With
    End Sub

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I'm sorry my fault. I was coping Megatrons code and must have been in a bit of a hurry. This is what the code should look like:
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        sIn.SelText = sWord
    End Function
    Good luck!

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Thumbs up

    Indeed it does. Thanks a lot.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Unhappy

    Sorry to bother you guys again. All of the code works now with one exception. When I have my String Value and insert it into the richtext box, it works fine except when I put it in at the end of a line. In which case it pops the next line up onto the same line as itself and messes the formatting of the file. Like this

    'Original file
    The cat sat on the mat
    The dog sat on the mat

    'We put $dog1 at the end of the first line, we want to see..

    The cat sat on the mat$dog1
    The dog sat on the mat

    'What we actually see is.......

    The cat sat on the mat£dog1 The dog sat on the mat

    Is there any way around this ?



  9. #9
    Member
    Join Date
    Jun 2000
    Posts
    37
    Yes, the problem is that you are overwriting the carriage return at the end of the line.

    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        If Right$(sIn.SelText, 1) = vbcrlf then
            sIn.SelText = sWord & vbCrLf
        Else
            sIn.SelText = sWord
        End If
    End Function

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Unhappy

    shub,
    That didn't work. It's doing the exact same thing.
    ????

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    vbCrLf is two characters change the code to this:
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        If Instr(sIn.SelText, vbCrLf) Then
            sIn.SelText = sWord & vbCrLf
        Else
            sIn.SelText = sWord
        End If
    End Function


  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Better

    Thanks, Its a bit better, but it still moves the next line back 3 characters

    eg:

    The cat sat on the mat$dog1
    The dog sat on the mat

    Do you know why this might be ?
    Thanks for your help.

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39
    Actually that example I just doesn't appear to have come out right , but you get what I mean, the whole second line would be shifted 3 spaces left.

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Ok try this then:
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        Dim iPos As Integer
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        iPos = Instr(sIn.SelText, vbCrLf)
        If iPos Then
            sIn.SelLength = iPos - 1
            sIn.SelText = sWord & vbCrLf
        Else
            sIn.SelText = sWord
        End If
    End Function

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Close

    Thats better all right, only one problem remains now, and that is that if I insert the text at the end, the next line keeps its formatting but the carraige return moves it on a line so you get :

    The cat sat on the mat$dog1

    The dog sat on the mat

    Instead of:

    The cat sat on the mat$dog1
    The dog sat on the mat

    I am starting to wonder, wether it is possible to do what it is I want to achieve.
    Thanks for your help though.

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    OK lets try this:
    Code:
    Function InsertWord(sIn As RichTextBox, sWord As String)
        Dim iPos As Integer
        sIn.SetFocus
        sIn.SelLength = Len(sWord)
        iPos = Instr(sIn.SelText, vbCrLf)
        If iPos Then
            sIn.SelLength = iPos - 1
            sIn.SelText = sWord 'dont add the vbCrLf
        Else
            sIn.SelText = sWord
        End If
    End Function
    Now this should work!!!

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Thumbs up Thanks

    Thanks Joacim, you are a star. It works great.

Posting Permissions

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



Click Here to Expand Forum to Full Width