Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete last character in textbox

Posted on 2012-03-15
5
Medium Priority
?
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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