1

I am making a number guessing game, and for convenience I would like to add a line of code to my program that selects all text within a text box with one click. I have tried everything I have found on here along with other trouble shooting sites I have found on google and none of it seems to work, even trying to force focus on the textbox. The textbox still behaves like a normal textbox, i.e. having to double click to select all.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace randomNumberGuessingGameFourthTry
{
     public partial class Form1 : Form
     {
         public Form1()
         {
          InitializeComponent();
         }

    private void startGame_Click(object sender, EventArgs e)
    {
        if (min.Text == "" || min.Text == " " || min.Text == "Min")
        {
            MessageBox.Show("You didn't enter a minimum value of zero or greater so the default value of 0 was set.");
            min.Text = "0";
        }

        if (max.Text == "" || max.Text == " " || max.Text == "Max")
        {
            MessageBox.Show("You didn't enter a maximum value so the default value of 10 was set.");
            max.Text = "11";
        }

        startGuessing startGame = new startGuessing(min.Text, max.Text);
        this.Hide();
        startGame.ShowDialog();
    }

   private void exitGame_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void min_TextChanged(object sender, EventArgs e)
    {
        min.Focus();
        min.SelectAll();
        min.SelectionLength = min.Text.Length;

        int userInput1 = Convert.ToInt32(min.Text);
        if (!(userInput1 >= 0))
        {
            MessageBox.Show("Your min range must be at least 0 or higher", "Invalid range found");
        }
    }

    private void max_TextChanged(object sender, EventArgs e)
    {


        int userInput1 = Convert.ToInt32(min.Text);
        int userInput2 = Convert.ToInt32(max.Text);
        if (!(userInput2 <= userInput1 + 9))
        {
            MessageBox.Show("Your max range must be at least 10 digits higher than " + userInput1, "Invalid range found");
        }
    }
}

}

The above is the code for my form1.cs I figured if I could make it work here I could make it work on my second form in this program.

SubZero
  • 43
  • 1
  • 6

2 Answers2

5

You should first call SelectAll() method then Focus(), not vice versa. This minimal example is working for me:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.Click += TextBoxOnClick;
    }

    private void TextBoxOnClick(object sender, EventArgs eventArgs)
    {
        var textBox = (TextBox) sender;
        textBox.SelectAll();
        textBox.Focus();
    }
}
Set
  • 47,577
  • 22
  • 132
  • 150
  • I have tried that. I don't know if im implementing it incorrectly or if my program is just simply ignoring the code – SubZero Sep 04 '16 at 02:35
  • @SubZero did you use Textbox.Click event? I have updated answer. – Set Sep 04 '16 at 02:49
  • Thank you, that worked exactly like I wanted it too. I was missing the "textBox1.Click += TextBoxOnClick;" line up top when I was trying to implement select all in the past. – SubZero Sep 04 '16 at 03:01
0

What did you try?

The following works fine for me:

private void textBox1_Click(object sender, EventArgs e)
{
    TextBox textBox = (TextBox)sender;

    textBox.SelectAll();
}

Naturally, you need to add textBox1_Click() as an event handler for your TextBox's Click event.

Note that the above will cause the text to always be completely selected with any mouse click in the control. This will make it impossible to select portions of the text using the mouse. If you want a more sophisticated behavior, you can use the answers found here:

Making a WinForms TextBox behave like your browser's address bar

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • I believe I have tried that too. But just to be sure since one of the text boxes in my code is named min I would implement your code by switching everything in your snippet with "textBox1" to "min" correct? – SubZero Sep 04 '16 at 02:50
  • @Sub: the name of the method can be anything you want. If your `TextBox` name is `min`, then you might prefer the method to be named `min_Click()`, but it will work regardless. The code I posted doesn't depend on any specific control name, and can in fact be used with more than one `TextBox` in your form if you like. You just have to make sure you actually add the method as a handler for the `Click` event (either subscribe explicitly in your form's constructor, or use the Designer to set the handler in the `TextBox`'s properties window). – Peter Duniho Sep 04 '16 at 03:25