按钮在C#中用作Backspace键 [英] Button to work as Backspace key in C#
问题描述
你好,
我在Windows窗体上工作,我正在使用一个richtextbox和一个按钮现在我的查询是什么,我想要在我的richtextbox上写的东西必须一个一个地消失点击该按钮
示例:假设我的richtextbox上写有123,然后点击该按钮前3必须消失然后2然后1 ...
我也启用了richtextboxs readonly属性为true并且我已将其最大长度设置为10
i尝试
Hello,
I am working on a windows form where i am taking a richtextbox and one button now what my query is that i want what so ever is written on my richtextbox must vanish one by one on the hit of that button
Example: lets say my richtextbox has 123 written on it then on the hit of that button first 3 must vanish and then 2 and then 1...
also i have enabled richtextboxs readonly property to true and i have set its max length to 10
i tried
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "\b";
}
但是它的工作失败了
but failed its working like
richTextBox1.Clear();
我也试过
also i tried
if (richTextBox1.TextLength != 0)
{
int x = richTextBox1.TextLength;
x += -1;
}
但我不知道该添加什么
ya如果光标位于中间,那么它必须逐一从当前位置删除文本
请帮帮我
谢谢&问候
Radix
but i dont know what to add further
ya and if the cursor is in the middle then too it must remove the text from its current location one by one
Please Please help me out
Thanks & Regards
Radix
推荐答案
你好
你写这个按钮的代码点击Back Back Space事件
Hello
You Write This Code Of Button's Click Event For Back Space
<br />
richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);<br />
希望寻求帮助
Hope For Help
跟踪光标的位置。可能有也可能没有这样的事件。如果没有,那么你将不得不检测按钮点击,按键和焦点变化以跟踪它。
使用string.Substring来提取部分字符串。一个你提取它(你需要两个string.Substrings,如果光标在文本的中间,所以你必须连接字符串),然后你将结果分配回richTextBox1.Text。
请记住,单击按钮将使焦点远离richtextbox。单击该按钮时,您将需要恢复焦点(并将光标的位置恢复到单击按钮之前的位置)。
Track the position of the cursor. There may or may not be an event for that. If not, then you'll have to detect button clicks, key presses, and focus changes to keep track of that.
Use string.Substring to extract a portion of the string. One you extract it (you'll need two string.Substrings if the cursor is in the middle of the text, so you will then have to concatenate the strings), you will then assign the result back to richTextBox1.Text.
Keep in mind a button click will take focus away from the richtextbox. When the button gets clicked, you will want to restore focus (and restore the position of the cursor to where it was before the button was clicked).
//这将有助于按钮作为退格按钮工作。在按钮的click事件上写下面的代码。
int index = richTextBox1.SelectionStart;
richTextBox1.Text = richTextBox1.Text。删除(richTextBox1.SelectionStart - 1,1);
richTextBox1.Select(index - 1,1);
richTextBox1.Focus();
//这对我有用。
//This will help the button to work as backspace button. On the click event of button write the following code.
int index = richTextBox1.SelectionStart;
richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.SelectionStart - 1, 1);
richTextBox1.Select(index - 1, 1);
richTextBox1.Focus();
//This has worked for me.
这篇关于按钮在C#中用作Backspace键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!