代码之家  ›  专栏  ›  技术社区  ›  Aidan Ryan

如何选择winforms numericupdown on tab in中的所有文本?

  •  50
  • Aidan Ryan  · 技术社区  · 16 年前

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

    6 回复  |  直到 7 年前
        1
  •  112
  •   Jon B    16 年前
    private void NumericUpDown1_Enter(object sender, EventArgs e)
    {
        NumericUpDown1.Select(0, NumericUpDown1.Text.Length);
    }
    

    (注意,文本属性在intellisense中是隐藏的,但它就在那里)

        2
  •  8
  •   BrinkDaDrink    10 年前

    我想为以后一直在搜索选项卡并单击的人添加此项。

    jon b answer适用于tab,但我需要修改以包含click

    如果您点击或点击,下面将选择文本。如果单击并输入框,则框将选择文本。如果你已经聚焦在盒子上,那么点击会像平常那样做。

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

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

        3
  •  4
  •   Kross    10 年前

    我环顾四周,发现了同样的问题,这对我很有用,首先选择项目,然后第二个选择文本,希望以后能有所帮助。

    myNumericUpDown.Select();
     myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);
    
        4
  •  2
  •   Aidan Ryan    10 年前

    我创建了一个扩展方法来实现这一点:

    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
  •  -1
  •   David Arenburg Ulrik    10 年前

    尝试

     myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);
    
        6
  •  -1
  •   Bugs Manjeet    8 年前

    我有多个numericupdown框,我想实现这一点。我创造了:

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

    然后,通过将此函数与每个框的Enter事件关联起来(我没有这样做),实现了我的目标。我花了一段时间才明白我是个初学者。希望这能帮助别人