C# 如何在选项卡中选择 Winforms NumericUpDown 中的所有文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/571074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:41:48  来源:igfitidea点击:

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

c#.netwinformsnumericupdown

提问by Aidan Ryan

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

当用户进入我的选项卡时,NumericUpDown我希望选择所有文本。这可能吗?

采纳答案by Jon B

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)

(注意 Text 属性隐藏在 Intellisense 中,但它在那里)

回答by cjbarth

I created an extension method to accomplish this:

我创建了一个扩展方法来完成这个:

VB:

VB:

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

C#:

C#:

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

回答by rony sc

Try

尝试

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

回答by Kross

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

回答by BrinkDaDrink

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

我想为将来搜索 Tab 和 Click 的人添加此内容。

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

Jon B 的答案非常适合 Tab,但我需要修改以包含点击

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.

如果您使用 Tab 或单击,下面将选择文本。如果您单击并输入框,则它将选择文本。如果您已经专注于该框,则单击将执行正常操作。

    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

您可以将它用于多个 numericUpDown 控件。只需要设置 Enter 和 MouseDown 事件

回答by Nicolas

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

我有多个 numericupdown 框,并希望为所有人实现这一目标。我创建:

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

然后通过将此函数与每个框的 Enter 事件相关联(我没有这样做),我的目标就实现了。我花了一段时间才弄清楚,因为我是初学者。希望这可以帮助其他人