Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
               
            }


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
Posted
Updated 16-Nov-20 19:52pm
v3
Comments
_Damian S_ 12-May-10 2:44am    
You say that you want the last character to be removed, but what if the cursor is in the middle of the text somewhere? Would you not want it removed where the cursor is situated?
radix3 12-May-10 3:05am    
Thank u Mr.Damian sir for helping me improve my question thank you

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
 
Share this answer
 
Comments
saloni15 27-May-10 1:04am    
Reason for my vote of 5
its easy to implement..
ranjitkumarr 3-Jan-13 7:57am    
nice
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).
 
Share this answer
 
//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.
 
Share this answer
 
C#
private void button1_Click(object sender, EventArgs e)
        {
            int i = richTextBox1.Text.Length;
            richTextBox1.Text = richTextBox1.Text.Substring(0, i - 1);
        }
 
Share this answer
 
And even one more alternative in case you always want to remove only the last char:
C#
richTextBox1.Text = richTextBox1.Text.Substring( 0, richTextBox1.Text.Length - 1 );
 
Share this answer
 
Comments
[no name] 5-Sep-13 7:04am    
Seriously? Do you really think that after 3 years the OP is still waiting for an answer?
Member 15594125 7-Apr-22 8:56am    
Great work :)
C#
if (focusedTextbox.SelectionStart>0)
          {
          int index = focusedTextbox.SelectionStart;
          focusedTextbox.Text = focusedTextbox.Text.Remove(focusedTextbox.SelectionStart - 1, 1);
          focusedTextbox.Select(index - 1, 1);
          focusedTextbox.Focus();
      }
 
Share this answer
 
Comments
[no name] 5-Sep-13 7:04am    
Seriously? Do you really think that after 3 years the OP is still waiting for an answer?
C#
if (richTextBox1.TextLength != 0)
            {
                richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);
            }
            else { return; }


This Worked for me
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 8:49am    
8 years too late!
CHill60 21-Jun-18 5:08am    
This is just a repost of a solution posted 8 years ago (Solution 2). Don't repost other people's solutions
Member 14597074 18-Sep-19 15:45pm    
dont mind those negative nancies your solutions worked perfectly
CHill60 17-Nov-20 11:27am    
Ha ha - of course this solution worked perfectly - it was copied from the accepted answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900