[英]How to capture the cursor position in a textbox?
I have a TextChanged event on my Masked TextBox and I want its method to be called just when the cursor stay at the end. 我在Masked TextBox上有一个TextChanged事件,我希望仅当光标停留在末尾时才调用其方法。
For example: 例如:
222.222.2/21 222.222.2 / 21
The event shall be called as soon as the user types "1". 一旦用户键入“ 1”,将立即调用该事件。
XAML XAML
<TextBox
Name="myTextBox"
ToolTip="type here"
Height="30"
Width="100"
FontSize="14"
MaxLength="12"
HorizontalContentAlignment="Right"
TextChanged="MyMethod"/>
C# C#
private void MyMethod(object sender, EventArgs e){
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
//how do I know if the cursor is at the end?
}
}
SOLUTION 解
private void MyMethod(object sender, EventArgs e){
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
if(process.CaretIndex == 12)
{
//do something
}
}
}
You can use myTextBox.CaretIndex
. 您可以使用
myTextBox.CaretIndex
。
private void MyMethod(object sender, EventArgs e)
{
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
System.Diagnostics.Debug.WriteLine($"caret is at {myTextBox.CaretIndex}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.