Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CTRL-A select all text in textBox

Posted on 2012-03-16
15
Medium Priority
?
685 Views
Last Modified: 2012-06-27
OK I would like CTRL-A (on keyboard) to select all the text in my textBox1. Right now CTRL-A does nothing, even though my cursor is in the textBox1.
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
  • 4
  • 2
  • +4
15 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 37729964
Is the text selectable?  CTRL-A should work by default, I believe...
0
 
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 300 total points
ID: 37729998
Hi deleyd,

Try this-
        private void textBox1_keyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.A)
                textBox1.SelectAll();
        }

Regards,
Rikin.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730016
Are you doing any key press filtering? Barring that, I'd have to agree with paulmacd.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:deleyd
ID: 37730033
OK I'll have a look at the textBox parameters. Maybe there's a setting somewhere I need to set from False to True.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730042
On further inspection, maybe it doesn't. I created a quick project, and it seems the key combination does not work as one would expect--though copy and paste key combinations did. It seems you may need rikin_shah's logic after all.
0
 

Author Comment

by:deleyd
ID: 37730044
Hmm, I don't see anything. I see AcceptsReturn and AcceptsTab. Nothing about selectable.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37730053
set the form's keypreview to true and handle the keyup event of the form. In there, check for the CTRL-A combination and also whether the textbox is currently selected then simply do copy textbox.text to clipboard
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730105
You shouldn't need KeyPreview for this task. KeyPreview is for the form to have a peek at (and possibly handle/suppress) the key being pressed before the control which is to process the key press receives the actual message stating a key was pressed.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 total points
ID: 37730237
Ctrl-A works for me:  VS2010 on Win 7.
0
 

Author Comment

by:deleyd
ID: 37730308
Hmm, very strange it works for Idle_Mind. I have VS2010 on Vista. CTRL-A works for other programs.

Here's my Form1.Designer.cs stripped down:
  partial class Form1
  {
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      //
      // textBox1
      //
      this.textBox1.Font = new System.Drawing.Font("Times New Roman", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.textBox1.Location = new System.Drawing.Point(9, 12);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(669, 52);
      this.textBox1.TabIndex = 48;
      //
      // textBox2
      //
      this.textBox2.AcceptsReturn = true;
      this.textBox2.AcceptsTab = true;
      this.textBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.textBox2.Location = new System.Drawing.Point(12, 92);
      this.textBox2.Multiline = true;
      this.textBox2.Name = "textBox2";
      this.textBox2.Size = new System.Drawing.Size(666, 50);
      this.textBox2.TabIndex = 51;
      //
      // Form1
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(703, 338);
      this.Controls.Add(this.textBox2);
      this.Controls.Add(this.textBox1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
  }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730483
Ctrl-A works for me:  VS2010 on Win 7.
My test was in XP, VS 2008. I didn't investigate too deeply, but the test was just a form with a TextBox--no logic.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37730512
I didn't delve too deeply either; just a form with two textboxes and a button (no code).  It's possible that placing a different type of control on the form could be causing this issue.
0
 

Author Comment

by:deleyd
ID: 37730530
OK I tested and found if I make a test form with two textBoxes, and both textBoxes are Multiline text boxes, then CTRL-A doesn't work for either textbox. I wonder why that is?
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 1400 total points
ID: 37730638
Yes, It's working even for me, My .net framework version is 2.0, IDE and OS doesn't matter in this case, the only thing is current focus should be on textbox

Here is the code for you

using System.Windows.Forms;

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

    
        public Form2()
        {
            InitializeComponent();
        }
      

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            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;
            // 
            // Form2
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.textBox1);
            this.Name = "Form2";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

      

    }
}

Open in new window


and if you want to  select all text in textbox even without having focus on textbox then you should set KeyPreview property to true and handle keyboard event on keydown like following code

using System.Windows.Forms;

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

    
        public Form2()
        {
            InitializeComponent();
        }
      

        private void InitializeComponent()
        {
            this.textBox1 = 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;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(34, 86);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            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 (textBox1.Focused == false && textBox1.Text.Length > 0 && e.KeyCode == Keys.A && e.Control== true)
            {
                textBox1.SelectAll();
                textBox1.Select();
            }
        }

       

      

    }
}

Open in new window

0
 

Author Closing Comment

by:deleyd
ID: 37731091
OK I worked around it for now using your ideas thank you for the help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 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