[英]How to remove the last character typed in a textbox
I want to remove the last character someone types in a textbox. 我想删除某人在文本框中键入的最后一个字符。 For example if someone types in an empty textbox "abcd", i want to remove the letter "d", or if in a textbox containing "abcd", the user types a 1 like in here: "ab1cd", i want to remove that 1
例如,如果有人在一个空的文本框中键入“ abcd”,我想删除字母“ d”,或者如果在一个包含“ abcd”的文本框中,用户在此处键入一个1,例如:“ ab1cd”,我要删除那1
Listen to the TextChanged event of the textbox, and save the change that happened: 侦听文本框的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);
}
This will remove the last letter and any kind of number in the string. 这将删除字符串中的最后一个字母和任何数字。 Hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.