[英]How to capture the cursor position in a textbox?
我在Masked TextBox上有一個TextChanged事件,我希望僅當光標停留在末尾時才調用其方法。
例如:
222.222.2 / 21
一旦用戶鍵入“ 1”,將立即調用該事件。
XAML
<TextBox
Name="myTextBox"
ToolTip="type here"
Height="30"
Width="100"
FontSize="14"
MaxLength="12"
HorizontalContentAlignment="Right"
TextChanged="MyMethod"/>
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?
}
}
解
private void MyMethod(object sender, EventArgs e){
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
if(process.CaretIndex == 12)
{
//do something
}
}
}
您可以使用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.