Joi*_*Paz 0 c# wpf events xaml 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"/>
Run Code Online (Sandbox Code Playgroud)
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?
}
}
Run Code Online (Sandbox Code Playgroud)
解
private void MyMethod(object sender, EventArgs e){
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
if(process.CaretIndex == 12)
{
//do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以用myTextBox.CaretIndex
.
private void MyMethod(object sender, EventArgs e)
{
if (myTextBox.Text.Length == myTextBox.MaxLength)
{
System.Diagnostics.Debug.WriteLine($"caret is at {myTextBox.CaretIndex}");
}
}
Run Code Online (Sandbox Code Playgroud)