Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete last character in textbox

Posted on 2012-03-15
5
Medium Priority
?
991 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 668 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 664 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 668 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

927 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