Results 1 to 10 of 10

Thread: [RESOLVED] get word(from textbox) on mousedown

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Resolved [RESOLVED] get word(from textbox) on mousedown

    i was trying to figure out this code by myself today and took me a bit before getting to this point

    vb Code:
    1. Private Function GetWord(TextBox_Name As TextBox) As String
    2. Dim WordFound As String
    3. Dim SplitWords() As String
    4. Dim WordFoundSplit() As String
    5. Dim DefaultStartPOS As Long
    6. Dim StartPOS As Long
    7. Dim WordPOS As Long
    8. Dim EndPOS As Long
    9. Dim i As Integer
    10.  
    11. On Error Resume Next
    12.  
    13. WordFound = ""
    14. StartPOS = 0
    15. EndPOS = 0
    16. WordPOS = 0
    17.  
    18. DefaultStartPOS = TextBox_Name.SelStart
    19. SplitWords = Split(TextBox_Name, Chr(32))
    20.  
    21. If DefaultStartPOS <= Len(SplitWords(0)) Then
    22.    TextBox_Name.SelStart = 0
    23.    TextBox_Name.SelLength = Len(SplitWords(0))
    24.    WordFound = TextBox_Name.SelText
    25.    TextBox_Name.SelLength = 0
    26.    TextBox_Name.SelStart = DefaultStartPOS
    27.    Me.Caption = WordFound
    28. Else
    29.    WordPOS = Len(SplitWords(0))
    30.    
    31.    For i = 1 To UBound(SplitWords)
    32.       StartPOS = WordPOS
    33.       WordPOS = WordPOS + Len(SplitWords(i)) + 1
    34.       EndPOS = WordPOS
    35.    
    36.       If DefaultStartPOS <= EndPOS And DefaultStartPOS > StartPOS Then
    37.          TextBox_Name.SelStart = WordPOS - Len(SplitWords(i))
    38.          TextBox_Name.SelLength = Len(SplitWords(i))
    39.          WordFound = TextBox_Name.SelText
    40.          TextBox_Name.SelLength = 0
    41.          TextBox_Name.SelStart = DefaultStartPOS
    42.          WordFoundSplit = Split(WordFound, vbNewLine)
    43.          WordFound = WordFoundSplit(0)
    44.          Exit For
    45.       End If
    46.    Next
    47. End If
    48.  
    49. GetWord = WordFound
    50. End Function
    51.  
    52. Private Sub txtScript_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    53. lblWord.Caption = GetWord(txtScript)
    54. End Sub


    is there a easier formula or easier way to find the word you click on in a textbox(multiline)?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: get word(from textbox) on mousedown

    Try this one
    vb Code:
    1. Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String
    2.  
    3.     Dim j As Long
    4.     Dim lngRightSpacePos As Long
    5.    
    6.     ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position.
    7.     For j = lngCursorPosition + 1 To Len(strText)
    8.         If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    9.             Exit For
    10.         End If
    11.     Next
    12.     lngRightSpacePos = j ' Save the position
    13.    
    14.    
    15.     ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position.
    16.     For j = lngCursorPosition To 1 Step -1
    17.         If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    18.             Exit For
    19.         End If
    20.     Next
    21.    
    22.     GetWord = Mid$(strText, j + 1, lngRightSpacePos - j)
    23.    
    24. End Function
    Last edited by 4x2y; Feb 11th, 2012 at 04:33 AM. Reason: Fix some bugs



  3. #3
    Addicted Member
    Join Date
    Jan 2009
    Location
    Mn-USA
    Posts
    168

    Re: get word(from textbox) on mousedown

    A comment from a guy who used to code REXX on IBM mainframes. There were three very useful functions that are missing in VB. "Word, Words, and Strip".
    The string function, 'Strip', returns a string with all extraneous multiple blanks(or optional char) removed, such that the string is segmented into 'words'.
    The Long function 'Words' returns the number of words in a string.
    The string function 'Word' returns the nth word from a string.

    With these three functions, just about all string manipulation was possible. I had to recreate these functions for myself in VB.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: get word(from textbox) on mousedown

    vb-only that would be very useful to do the word, word, stip functions something i might think about doing, if you don't mind can you post me an example of your code or how you did your code maybe?

    and 4x2y thanks for the code it works good only one little problem when i get the vbnewline for some reason the first word after vbnewline stays with the vbnewline which is only vbLF so i modified it like this

    vb Code:
    1. Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String
    2. Dim j As Long
    3. Dim SplitWords() As String
    4. Dim lngRightSpacePos As Long
    5. ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position.
    6. For j = lngCursorPosition + 1 To Len(strText)
    7. If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    8. Exit For
    9. End If
    10. Next
    11. lngRightSpacePos = j ' Save the position
    12. ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position.
    13. For j = lngCursorPosition To 1 Step -1
    14. If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    15. Exit For
    16. End If
    17. Next
    18.  
    19. Word = Mid$(strText, j + 1, lngRightSpacePos - j)
    20.  
    21. If Word = vbLf & Right(Word, Len(Word) - 1) Then
    22. Word = Right(Word, Len(Word) - 1)
    23. End If
    24.  
    25. GetWord = Word
    26. End Function

    can you change that a different way in your sub?
    i tried to find out why but this is the solution i got to, and it works perfectly now

    thanks!

  5. #5
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: get word(from textbox) on mousedown

    There is an API that finds the first character from cursor.
    From there you should be able to search forward to get the word.
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    
    Private Type POINTAPI
    	x As Long
    	y As Long
    End Type
    
    Private Const EM_CHARFROMPOS As Long = &HD7
    
    Function CharFromPos(ByVal hWnd As Long, ByVal xPixels As Long, ByVal yPixels As Long) As Long
       Dim tP As POINTAPI
       tP.X = xPixels
       tP.Y = yPixels
       CharFromPos = SendMessage(hWnd, EM_CHARFROMPOS, 0, tP)
    End Property
    Last edited by DrUnicode; Feb 11th, 2012 at 11:54 PM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: get word(from textbox) on mousedown

    i get -1 in return everytime i try your function DrUnicode

    vb Code:
    1. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    2.  
    3. Private Type POINTAPI
    4.    X As Long
    5.    Y As Long
    6. End Type
    7.  
    8. Private Const EM_CHARFROMPOS As Long = &HD7
    9.  
    10. Function CharFromPos(ByVal hWnd As Long, ByVal xPixels As Long, ByVal yPixels As Long) As Long
    11.    Dim tP As POINTAPI
    12.    tP.X = xPixels
    13.    tP.Y = yPixels
    14.    CharFromPos = SendMessage(hWnd, EM_CHARFROMPOS, 0, tP)
    15. End Function
    16.  
    17. Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    18. Me.Caption = CharFromPos(Text1.hWnd, X, Y)
    19. End Sub

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: get word(from textbox) on mousedown

    Quote Originally Posted by Max187Boucher View Post
    and 4x2y thanks for the code it works good only one little problem when i get the vbnewline for some reason the first word after vbnewline stays with the vbnewline which is only vbLF so i modified it like this

    vb Code:
    1. Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String
    2. Dim j As Long
    3. Dim SplitWords() As String
    4. Dim lngRightSpacePos As Long
    5. ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position.
    6. For j = lngCursorPosition + 1 To Len(strText)
    7. If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    8. Exit For
    9. End If
    10. Next
    11. lngRightSpacePos = j ' Save the position
    12. ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position.
    13. For j = lngCursorPosition To 1 Step -1
    14. If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    15. Exit For
    16. End If
    17. Next
    18.  
    19. Word = Mid$(strText, j + 1, lngRightSpacePos - j)
    20.  
    21. If Word = vbLf & Right(Word, Len(Word) - 1) Then
    22. Word = Right(Word, Len(Word) - 1)
    23. End If
    24.  
    25. GetWord = Word
    26. End Function

    can you change that a different way in your sub?
    i tried to find out why but this is the solution i got to, and it works perfectly now
    The solution is simple, just replace GetWord = Mid$(strText, j + 1, lngRightSpacePos - j) with GetWord = Mid$(strText, j + 1, lngRightSpacePos - j - 1)

    vb Code:
    1. Private Function GetWord(ByVal strText As String, ByVal lngCursorPosition As Long) As String
    2.  
    3.     Dim j As Long
    4.     Dim lngRightSpacePos As Long
    5.    
    6.     ' Get the position of first <Space> or <NewLine> comes AFTER the cursor position.
    7.     For j = lngCursorPosition + 1 To Len(strText)
    8.         If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    9.             Exit For
    10.         End If
    11.     Next
    12.     lngRightSpacePos = j ' Save the position
    13.    
    14.    
    15.     ' Get the position of first <Space> or <NewLine> comes BEFORE the cursor position.
    16.     For j = lngCursorPosition To 1 Step -1
    17.         If Mid$(strText, j, 1) = " " Or Mid$(strText, j, 2) = vbNewLine Then
    18.             Exit For
    19.         End If
    20.     Next
    21.    
    22.     GetWord = Mid$(strText, j + 1, lngRightSpacePos - j - 1)
    23.    
    24. End Function



  8. #8
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: get word(from textbox) on mousedown

    Sorry, should have tested this first.
    You can also use Text1.SelStart whcih appears to return the same as Function CharFromPos.

    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    
    Private Const EM_CHARFROMPOS As Long = &HD7
    
    Function CharFromPos(ByVal hWnd As Long, ByVal xPixels As Long, ByVal yPixels As Long) As Long
       Dim lngRet As Long
       Dim lngVal As Long
    
       lngVal = MakeDWord(CInt(xPixels), CInt(yPixels))
       CharFromPos = LOWORD(SendMessage(hWnd, EM_CHARFROMPOS, 0, ByVal lngVal))
    End Function
    
    Function MakeDWord(LOWORD As Integer, HIWORD As Integer) As Long
       MakeDWord = (HIWORD * &H10000) Or (LOWORD And &HFFFF&)
    End Function
    
    Function LOWORD(DWord As Long) As Integer
       If DWord And &H8000& Then
          LOWORD = DWord Or &HFFFF0000
       Else
          LOWORD = DWord And &HFFFF&
       End If
    End Function
    
    Private Sub Form_Load()
       Me.Show
       Text1.Text = "Hello" & vbCrLf & "World"
    End Sub
    
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
       Dim lChar As Long
       Dim sChar As String
       Dim iChar As Integer
       
       lChar = CharFromPos(Text1.hWnd, X \ Screen.TwipsPerPixelX, Y \ Screen.TwipsPerPixelY)
       sChar = Mid$(Text1.Text, lChar + 1, 1)
       If Len(sChar) Then
          iChar = Asc(sChar)
       Else
          iChar = 0
       End If
       Me.Caption = lChar & ", " & sChar & ", " & iChar
    End Sub
    Last edited by DrUnicode; Feb 12th, 2012 at 02:21 PM. Reason: SelStart

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: get word(from textbox) on mousedown

    Quote Originally Posted by DrUnicode View Post
    There is an API that finds the first character from cursor.
    From there you should be able to search forward to get the word.
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    
    Private Type POINTAPI
    	x As Long
    	y As Long
    End Type
    
    Private Const EM_CHARFROMPOS As Long = &HD7
    
    Function CharFromPos(ByVal hWnd As Long, ByVal xPixels As Long, ByVal yPixels As Long) As Long
       Dim tP As POINTAPI
       tP.X = xPixels
       tP.Y = yPixels
       CharFromPos = SendMessage(hWnd, EM_CHARFROMPOS, 0, tP)
    End Property

    This work for RichTextBox not TextBox, beside it doesn't help to get the word under the cursor because it just return the cursor position inside the RichTextBox, it is simulate RichTextBox1.SelStart



  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: get word(from textbox) on mousedown

    thanks alot guys now i understand how it works better
    Drunicode thanks for the API
    4x2y thanks for your code also... my code had me a bit confused at some point but with your code now is easier to read !

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