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 boxKeyboard.Focus(txtLineItemUnits);
Please suggest me any working code.... pls
解决方案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);