[英]How to remove the last character typed in a textbox
我想刪除某人在文本框中鍵入的最后一個字符。 例如,如果有人在一個空的文本框中鍵入“ abcd”,我想刪除字母“ d”,或者如果在一個包含“ abcd”的文本框中,用戶在此處鍵入一個1,例如:“ ab1cd”,我要刪除那1
偵聽文本框的TextChanged事件,然后保存發生的更改:
public partial class MainWindow : Window
{
private ICollection<TextChange> _latestChange = null;
public MainWindow()
{
InitializeComponent();
myTextBox.TextChanged += (o, a) =>
{
_latestChange = a.Changes;
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_latestChange != null)
{
var change = _latestChange.FirstOrDefault(); // Just take first change
if (change.AddedLength > 0) // If text was removed, ignore
{
myTextBox.Text = myTextBox.Text.Remove(change.Offset, change.AddedLength);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string newstring = textBox1.Text;
for(int i = 0; i<=9; i++)
{
if(newstring.Contains(i.ToString()))
{
int start = newstring.IndexOf(i.ToString());
newstring = newstring.Remove(start, 1);
}
}
textBox1.Text = newstring.Remove(newstring.Length - 1);
}
這將刪除字符串中的最后一個字母和任何數字。 希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.