Results 1 to 7 of 7

Thread: character position in richtextbox

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    character position in richtextbox

    Simple:

    How do I get the position of an ascii character in the richtextbox when user enters it?

    For example:

    hello

    If I enter e after I have entered "hllo", it should return 2 as the character position.
    Baaaaaaaaah

  2. #2
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    ritchtextbox1.selstart

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Damn It!
    I though "selstart" property was never changed unless you change it yourself.

    Thanks a lot dude!
    Baaaaaaaaah

  4. #4
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    If you want the position just as the letter is entered you can use the RichTextBox.SelStart property. If nothing is selected it returns the position of the cursor. If you want to look for something after the fact you can use the Instr() function. Below are some simple examples. You will need to test and debug.

    VB Code:
    1. Private Sub RichTextBox_KeyPress(KeyAscii As Integer)
    2.      'Lower case e
    3.     If KeyAscii = 101 Then
    4.          lSpot = RichTextBox.SelStart
    5.     End If
    6. End Sub

    VB Code:
    1. Dim lSpot As Long
    2.  
    3. lSpot = Instr(RichTextBox.Text, " hello ")
    4. 'Add 2 for the e
    5. lSpot = lSpot + 2

    If there is more than one instance of " hello " this will find the first one. You can use InstrRev to do the samething but find the last occurance.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I am actually working on a code colouring textbox. So, it will be hopefully working that way.
    Baaaaaaaaah

  6. #6
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Originally posted by abdul
    I am actually working on a code colouring textbox. So, it will be hopefully working that way.
    let me know when you finish it!

  7. #7

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    ok
    Baaaaaaaaah

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