Solved

delete last character in textbox

Posted on 2012-03-15
5
926 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now