从文本框中删除最后输入的字符 [英] Remove last entered character from a text box

查看:110
本文介绍了从文本框中删除最后输入的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

如何从文本框中实用地删除最后输入的字符(不一定是字符串的末尾).

问候
Sebastian

Hello,

How I can pragmatically remove last entered character(It may not be necessarily at the end of the string) from a text box.

Regards
Sebastian

推荐答案

除"undo"外,没有其他内置机制-可以删除的不仅仅是最后一个字符.
不过,您也许可以自己做一些事情.
我将基于TextBox创建自己的控件,并在其中添加Stack< string>以私人身份加入.
处理TextChanged事件,并将新值压入堆栈.
当您想撤消最后一个字符时,弹出最高值,将其丢弃.弹出下一个值,然后将文本框文本设置为该值. (这将导致TextChanged事件,将其重新放回原处)

显然,您将需要检查是否为空,依此类推,但这就是我要做的方式.
There is no inbuilt mechanism other than "undo" - which may remove more than just the last character.
You may be able to do something on your own though.
I would create my own control, based on a TextBox and add a Stack<string> to it as a private member.
Handle the TextChanged event and push the new value on the stack.
When you want to undo the last character, pop off the top value, discard it. Pop off the next value, and set the textbox text to that. (This will cause a TextChanged event, which will put it right back on again)

Clearly, you will need to check for empty, and so forth, but that''s the way I''d do it.


您是否尝试过?
Have you tried this?
textBox1.Text = textBox1.Text.SubString(0, textBox1.Text.Lenght - 1);


这篇关于从文本框中删除最后输入的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆