wpf 修改输入框 光标_wpf c#中文本框中的光标位置

在WPF C#应用程序中处理DataGrid中的文本框事件时,当用户输入字符后,光标焦点丢失。本文讨论如何使用TextBox.SelectionStart和TextBox.SelectionLength属性控制文本框中的光标位置,以及在更新文本后如何恢复光标位置。
摘要由CSDN通过智能技术生成

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值