How to select all text in Winforms NumericUpDown upon tab in?

谁都会走 提交于 2019-11-28 22:41:23

问题


When the user tabs into my NumericUpDown I would like all text to be selected. Is this possible?


回答1:


private void NumericUpDown1_Enter(object sender, EventArgs e)
{
    NumericUpDown1.Select(0, NumericUpDown1.Text.Length);
}

(Note that the Text property is hidden in Intellisense, but it's there)




回答2:


I wanted to add to this for future people who have been search for Tab and Click.

Jon B answer works perfect for Tab but I needed to modify to include click

Below will select the text if you tab in or click in. If you click and you enter the box then it will select the text. If you are already focused on the box then the click will do what it normally does.

    bool selectByMouse = false;

    private void quickBoxs_Enter(object sender, EventArgs e)
    {
        NumericUpDown curBox = sender as NumericUpDown;
        curBox.Select();
        curBox.Select(0, curBox.Text.Length);
        if (MouseButtons == MouseButtons.Left)
        {
            selectByMouse = true;
        }
    }

    private void quickBoxs_MouseDown(object sender, MouseEventArgs e)
    {
        NumericUpDown curBox = sender as NumericUpDown;
        if (selectByMouse)
        {
            curBox.Select(0, curBox.Text.Length);
            selectByMouse = false;
        }
    }

You can use this for multiple numericUpDown controls. Just need to set the Enter and MouseDown Events




回答3:


I was looking around i had the same issue and this Works for me, first select the Item and the second one selects the Text, hope it helps in future

myNumericUpDown.Select();
 myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);



回答4:


I created an extension method to accomplish this:

VB:

<Extension()>
Public Sub SelectAll(myNumericUpDown As NumericUpDown)
    myNumericUpDown.Select(0, myNumericUpDown.Text.Length)
End Sub

C#:

public static void SelectAll(this NumericUpDown numericUpDown)
    numericUpDown.Select(0, myNumericUpDown.Text.Length)
End Sub



回答5:


I had multiple numericupdown box's and wanted to achieve this for all. I created:

private void num_Enter(object sender, EventArgs e)
{
    NumericUpDown box = sender as NumericUpDown;
    box.Select();
    box.Select(0, num_Shortage.Value.ToString().Length);
}

Then by associating this function with the Enter Event for each box (which I didn't do), my goal was achieved. Took me a while to figure out as I am a beginner. Hope this helps someone else out




回答6:


Try

 myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);


来源:https://stackoverflow.com/questions/571074/how-to-select-all-text-in-winforms-numericupdown-upon-tab-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!