简体   繁体   English

有没有一种方法可以在c#winform中的文本框中单击一次全部选择?

[英]Is there a way to select all with one click in a textbox in c# winform?

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. 我已经尝试过在这里找到的所有内容以及在Google上找到的其他故障排除网站,但似乎都没有用,甚至试图将精力集中在文本框上。 The textbox still behaves like a normal textbox, ie 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. 上面是我的form1.cs的代码,我认为如果可以在这里运行,可以在此程序的第二个窗体上运行。

You should first call SelectAll() method then Focus() , not vice versa. 您应该先调用SelectAll()方法,然后调用Focus() ,而不是相反。 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();
    }
}

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. 自然,您需要将textBox1_Click()添加为TextBoxClick事件的事件处理程序。

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 使WinForms TextBox的行为类似于浏览器的地址栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM