Solved

delete last character in textbox

Posted on 2012-03-15
5
931 Views
Last Modified: 2012-03-16
I have a textbox, textBox1, with some text in it. User presses 'Delete' key, I want to delete last character in textbox1. What is command to do that?

(sort of the opposite of textBox1.AppendText)

(cursor is not in textbox. Actually I have two textboxes I need to remove last character from.)
0
Comment
Question by:deleyd
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37728055
Where does the user press delete ? On our form, in a textbox , ...

textBox1.Text.Remove(textBox1.Text.Length - 1, 1);



HOW TO: Remove Last Character from String
http://blogs.x2line.com/al/archive/2008/03/10/3369.aspx
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 167 total points
ID: 37728069
KeyPreview  to true in the Form1

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Delete && textBox1.Focused==false)
                textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
        }
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 166 total points
ID: 37728122
Here is another way to do it:-

private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Delete && textBox1.Focused==false)
            {
                var textRead = textBox1.Text;
                textBox1.Text = textRead .Remove(textRead .Length-1)
                //OR
                //textBox1.Text=textRead .Substring(0,textRead .Length-1);
           }
         }

Open in new window

0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 167 total points
ID: 37728356
Here is code for this:

for this you need to set the KeyPreview property of container(in my case form) control to true and put your logic for last character removal.

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private TextBox textBox1;
        private TextBox textBox2;
        private Button button1;

    
        public Form2()
        {
            InitializeComponent();
        }
      

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(23, 44);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(139, 44);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(88, 91);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form2
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.KeyPreview = true;
            this.Name = "Form2";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form2_KeyDown);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (Control control in Controls)
                {
                    if (control is TextBox)
                    {
                        TextBox textBox = control as TextBox;

                        RemoveLastCharacter(textBox);
                    }
                }
            }
        }

        private void RemoveLastCharacter(TextBox textBox)
        {

            if (textBox.Text.Length > 0)
            {
                textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
            }
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:deleyd
ID: 37729498
textBox has a .Remove, that's what I needed.

Thank you everyone!

Oh I see, it's textBox.Text that has the .Remove

That's why I couldn't find it. Hmm, still can't find it. Oh I bet I have to look at String.Remove

textBox.Text gives me a string. Ah yes, that's it.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question