[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? :wave:
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. :(
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:
Private Function GetDeltaCharPosition(ByVal textHwnd As Long, ByVal currentCharDelta As Long, ByVal currentLine As Long, ByVal arrowDirection As Direction) As Long
Dim newLine As Long
Dim newCharLineStart As Long, currentCharLineStart As Long
Dim newWidth As Long
Dim currentWidth As Long
If arrowDirection = Direction.Up Then
newLine = currentLine - 1
Else
newLine = currentLine + 1
End If
newCharLineStart = SendMessageSetLong(textHwnd, EM_LINEINDEX, newLine, 0)
currentCharLineStart = SendMessageSetLong(textHwnd, EM_LINEINDEX, currentLine, 0)
newWidth = SendMessageSetLong(textHwnd, EM_LINELENGTH, newCharLineStart, 0)
currentWidth = currentCharDelta - currentCharLineStart
If currentWidth <= newWidth Then
GetDeltaCharPosition = newCharLineStart + currentWidth
Else
GetDeltaCharPosition = newCharLineStart + newWidth
End If
End Function