WPF模拟键盘输入和删除
2021-07-03 10:15
标签:rgs .com rem return sha inf 分享 http content WPF模拟键盘输入和删除 标签:rgs .com rem return sha inf 分享 http content 原文地址:https://www.cnblogs.com/sntetwt/p/9897630.htmlprivate void ButtonNumber_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
string number = btn.Content.ToString();
string keyword = txtKeyword.Text;
//已经存在点"."
if (keyword.Contains(".") && number.Equals("."))
{
txtKeyword.Focus();
return;
}
//当前焦点位置
int start = txtKeyword.SelectionStart;
//焦点后面添加内容
if (Common.Utils.isNumber(number) || number.Equals("."))
{
txtKeyword.Text = keyword.Insert(start, number);
txtKeyword.SelectionStart = (start + number.Length);
}
else
{
//删除焦点前面的内容
if (start > 0)
{
txtKeyword.Text = txtKeyword.Text.Remove(start - 1, 1);
txtKeyword.SelectionStart = start - 1;
}
}
txtKeyword.Focus();
}