Simulate up arrow in multi-line textbox

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I am writing a little 'macro' functionality into my multi-line textbox. All is ok except i don't know how to move the cursor up, or down, lines to simulate pressing the keyboard arrow keys (left and right are no problem). Obviously lines can wrap so using substring may be out?
Can someone please point me in the right direction? I would like to avoid the whole 'SendKeys' thing if possible...

(vb.net 2010, framework 4)
 
A multilined TextBox will expose its text splitted into an array of string, one element per line, inside the Lines property. Now, it seems that if you iterate this array and store the length of each line into an array of integer, and compare these figures with the cursor position given by SelectionStart property, you will be able to determine in which line the cursor is placed, and then set a new value for SelectionStart, in order to move it the exact amount of positions back or ahead, in order to reach a different line. Hope it helps. Kind regards...
 
Firstly, Thanks for your reply. With the number of views and lack of responses it seems this is a more difficult issue than meets the eye!
Regarding your proposal, this would work fine if the text was not word-wrapped. I may have a text file where the first ten on-screen lines are really only one line that is wrapped. There would also be an issue with variable width fonts? May well be that i am reading your idea incorrectly as I am a .net novice and really struggling with this. Please advise further if i am wrong.

I have given in and started playing with the SendKeys option, but only for {UP}, {DOWN}, {END} and {HOME}. Problem with this is that i appear to lose the .SelectionStart whenever SendKeys is used (depite using .Focus)...

A multilined TextBox will expose its text splitted into an array of string, one element per line, inside the Lines property. Now, it seems that if you iterate this array and store the length of each line into an array of integer, and compare these figures with the cursor position given by SelectionStart property, you will be able to determine in which line the cursor is placed, and then set a new value for SelectionStart, in order to move it the exact amount of positions back or ahead, in order to reach a different line. Hope it helps. Kind regards...
 
Last edited:
If you get index immediately after using Send method the message may not have been processed yet, to ensure it has use the SendWait method.
 
You are right, there was a misunderstanding from my own about how property "Lines" splits the text. MSDN library (Lines Property) clarifies that it looks for "a newline character". That, in my point of view, makes this property quite useless, for the same result could be reached by Split(TextBox1.Text, vbLf). A worse way to accomplish what I suggested (that is, with no use of SendKeys) is having your TextBox formatted with a Fixed-Width Font, and then adding or diminishing the .SelectionStart value by the exact number of characters that fulfill an entire line... Kind regards!
 
Back
Top