Getting the current "word"

Topics: User Forum
Apr 24, 2008 at 5:57 PM
I want to provide context-sensitive help. If the editor contains selected text then this is easy (use the selected text) - otherwise I want to use the current word where the cursor is, basically the same text that would be selected if I double-clicked at that point.

Any suggestions on how to get this text?
Jun 3, 2008 at 7:20 PM
The following should work, but does not because of a problem with the GetWordFromPosition function. It will probably be fixed at some point and will be the best solution.

    Private Sub Scintilla1_HelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles Scintilla1.HelpRequested
        Dim iPosition As Integer = Scintilla1.Caret.Position
        Dim sWord As String = Scintilla1.GetWordFromPosition(iPosition)
        MsgBox(sWord)
     End Sub

The following performs the same function:

    Private Sub Scintilla1_HelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles Scintilla1.HelpRequested
        Dim iPosition As Integer = Scintilla1.Caret.Position

        Dim startPosition As Integer = Scintilla1.NativeInterface.WordStartPosition(iPosition, True)
        Dim endPosition As Integer = Scintilla1.NativeInterface.WordEndPosition(iPosition, True)
        dim sWord as string = Scintilla1.GetRange(startPosition, endPosition).Text

        MsgBox(sWord)
    End Sub

Steve
Jun 4, 2008 at 9:39 AM
Many thanks.

We're currently using version V6-b1.1 beta, so will upgrade to V2.0 soon and I will try out the suggestion.


Regards

John
Jun 6, 2008 at 5:23 PM
Upgraded to Version 2-0, and this works a treat. Used the 2nd example given the problems with GetWordFromPosition().

Thanks for the advice


John
Coordinator
Jul 29, 2008 at 7:06 AM
Fixed GetWordFromPosition with Changeset 34535. Thanks, Stumpii!