vb.net 单击按钮时从文本框中删除最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26611783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:25:03  来源:igfitidea点击:

Remove last character from textbox on button click

vb.nettextboxbackspace

提问by Kashish Arora

I have a textbox, textbox1, whose ReadOnlyproperty is set to true. It is somewhat like an on-screen keyboard. When the user presses a button, I can add characters by using:

我有一个textbox, textbox1,其ReadOnly属性设置为 true。它有点像屏幕键盘。当用户按下按钮时,我可以使用以下方法添加字符:

Textbox1.Text = Textbox1.Text & A

But, when the user wants to remove the text, which should work exactly like the Backspace key, the last character should be removed from the textbox.

但是,当用户想要删除应该与Backspace 键完全相同的文本时,应该从textbox.

I am ready to give up normal textboxand use richtextboxif there is any way ahead with the rich one.

我准备放弃正常textbox使用,richtextbox如果有任何方法可以使用富人。

How to remove last character of a textbox with a button?

如何使用按钮删除文本框的最后一个字符?

回答by Cody Popham

The previously suggested SubStringmethod is what I would have posted too. However, there is an alternative. The Removemethod works by removing every character after the given index. For instance:

之前建议的SubString方法也是我会发布的。但是,还有一种选择。该Remove方法的工作原理是删除给定索引后的每个字符。例如:

TextBox1.Text = Textbox1.Text.Remove(TextBox1.Text.Length - 1)

回答by Steven Doggart

You can use the SubStringmethod to get all but the last character, for instance:

您可以使用该SubString方法获取除最后一个字符之外的所有字符,例如:

TextBox1.Text = TextBox1.Text.SubString(0, TextBox1.Text.Length - 1)

回答by Mnt.Jafarzadeh

The above functions such as Substringand Removeare not executed in Visual Basic 6 (or earlier versions). I recommend to use Midfunction.

上述功能如SubstringRemove在 Visual Basic 6(或更早版本)中不执行。我建议使用Mid函数。

For instance,in this example last character of Hellois deleted and rest of word is saved in Text1

例如,在这个例子中,最后一个字符Hello被删除,剩下的单词被保存在Text1

Text1=Mid("Hello",1, Len("Hello") - 1)

and result is Text1=Hell

结果是 Text1=Hell