Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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 11-May-10 21:34pm
radix32.1K
Edited 11-May-10 22:04pm
v3
Comments
_Damian S_ at 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 at 12-May-10 3:05am
   
Thank u Mr.Damian sir for helping me improve my question thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello
 
You Write This Code Of Button's Click Event For Back Space
 

richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);

 

Hope For Help
  Permalink  
Comments
saloni15 at 27-May-10 1:04am
   
Reason for my vote of 5
its easy to implement..
ranjitkumarr at 3-Jan-13 7:57am
   
nice
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

how to remove the character in textbox depends cursor position??
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

//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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

private void button1_Click(object sender, EventArgs e)
        {
            int i = richTextBox1.Text.Length;
            richTextBox1.Text = richTextBox1.Text.Substring(0, i - 1);
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

And even one more alternative in case you always want to remove only the last char:
richTextBox1.Text = richTextBox1.Text.Substring( 0, richTextBox1.Text.Length - 1 );
  Permalink  
Comments
ThePhantomUpvoter at 5-Sep-13 7:04am
   
Seriously? Do you really think that after 3 years the OP is still waiting for an answer?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

if (focusedTextbox.SelectionStart>0)
          {
          int index = focusedTextbox.SelectionStart;
          focusedTextbox.Text = focusedTextbox.Text.Remove(focusedTextbox.SelectionStart - 1, 1);
          focusedTextbox.Select(index - 1, 1);
          focusedTextbox.Focus();
      }
  Permalink  
Comments
ThePhantomUpvoter at 5-Sep-13 7:04am
   
Seriously? Do you really think that after 3 years the OP is still waiting for an answer?

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 5 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100