且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按钮在C#中用作Backspace键

更新时间:2023-12-02 22:18:34

你好



你写这个按钮的代码点击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.