Solved

delete last character in textbox

Posted on 2012-03-15
5
938 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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