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

时光怂恿深爱的人放手 提交于 2019-11-30 01:10:58
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)

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

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);
cjbarth

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
Nicolas

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

rony sc

Try

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