Click here to Skip to main content
14,699,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i am using Datagrid in wpf c# windows application, I am handling antext box event in datagrid wpf.... when user enters a key in text box the change event willl be called... But after that event the Focus of the cursor is being lost... in the case again the user should go to the text box and enter the value... Can anyone suggest how to position the cursor in a text box after the string.... I use the followin code where the cursor is placing at the starting of text box


Keyboard.Focus(txtLineItemUnits);



Please suggest me any working code.... pls
Posted

1 solution

There is some mixing up going on in most of times between the terms "caret" and "cursor". Cursor is the mouse cursor, caret is the vertical blinking bar inside the textbox that indicates the insertion point.
You can control caret position (and selection) by
TextBox.SelectionStart
and
TextBox.SelectionLength
properties.
Example if you want move caret before 3th character set
SelectionStart = 2
and
SelectionLength = 0


If you mean the mouse cursor and the caret at the same time, then you can use a code like:

// save current cursor position and selection 
        int start = textBox.SelectionStart;
        int length = textBox.SelectionLength;

        Point point = new Point();
        User32.GetCaretPos(out point);

        // update text
        textBox.Text = value;

        // restore cursor position and selection
        textBox.Select(start, length);
        User32.SetCaretPos(point.X, point.Y);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900