Results 1 to 3 of 3

Thread: [RESOLVED] Implementing my own arrow key navigation on a textbox

  1. #1

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Resolved [RESOLVED] Implementing my own arrow key navigation on a textbox

    Due to some weird circumstance, I am now having to manipulate the arrow key navigation on an active textbox. I figured I can use the API to get information on the lines... Here is my unfinished code snippet

    Code:
    Dim startSel As Long, endSel As Long
        Dim delta As Long, lineCnt As Long, currentLine As Long
        
        'Get the initial values
        SendMessageLong activeTextBox.hwnd, EM_GETSEL, startSel, endSel
        delta = endSel
        'lines are 0-based
        lineCnt = SendMessageLong(activeTextBox.hwnd, EM_GETLINECOUNT, 0, 0)
        currentLine = SendMessageLong(activeTextBox.hwnd, EM_LINEFROMCHAR, delta, 0)
        'calculate delta
        Select Case KeyCode
            Case vbKeyUp
                'calculate up
                If currentLine > 0 Then
                    
                End If
            Case vbKeyDown
                'calculate down
                If currentLine < lineCnt - 1 Then
                    
                End If
            Case vbKeyLeft
                'calculate left
                If delta > 0 Then
                    delta = delta - 1
                End If
            Case vbKeyRight
                'calculate right
                delta = delta + 1
        End Select
    Now for the KeyUp and Keydown, I need to place the current cursor into the correct line. Currently, what I have in mind is to calculate the character index for each line and get the delta character change and use that to send an api call for EM_SETSEL.

    Basically what I'd do is, call sendmessage on EM_GETLINE, get all character count, determine the line where delta should fall, determine the new char index from that line and then add the character counts of all preceding lines, and that will be my delta.

    Is there another straightforward API call for this? or some other clever way perhaps?

  2. #2

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Implementing my own arrow key navigation on a textbox

    Well, I figured out that I have to keep track of the achor for SHIFT+Arrow keys to behave at least in an acceptable manner.

    If nobody has other ideas, then I suppose i'd have to go with my idea then.

  3. #3

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Implementing my own arrow key navigation on a textbox

    Found the solution! and I didn't have to loop which is awesome.

    All SendMessageSetLong is a SendMessage Api call alias.

    vb Code:
    1. Private Function GetDeltaCharPosition(ByVal textHwnd As Long, ByVal currentCharDelta As Long, ByVal currentLine As Long, ByVal arrowDirection As Direction) As Long
    2.     Dim newLine As Long
    3.     Dim newCharLineStart As Long, currentCharLineStart As Long
    4.     Dim newWidth As Long
    5.     Dim currentWidth As Long
    6.    
    7.     If arrowDirection = Direction.Up Then
    8.         newLine = currentLine - 1
    9.     Else
    10.         newLine = currentLine + 1
    11.     End If
    12.    
    13.     newCharLineStart = SendMessageSetLong(textHwnd, EM_LINEINDEX, newLine, 0)
    14.     currentCharLineStart = SendMessageSetLong(textHwnd, EM_LINEINDEX, currentLine, 0)
    15.    
    16.     newWidth = SendMessageSetLong(textHwnd, EM_LINELENGTH, newCharLineStart, 0)
    17.     currentWidth = currentCharDelta - currentCharLineStart
    18.    
    19.     If currentWidth <= newWidth Then
    20.         GetDeltaCharPosition = newCharLineStart + currentWidth
    21.     Else
    22.         GetDeltaCharPosition = newCharLineStart + newWidth
    23.     End If
    24.    
    25. End Function

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