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

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

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

    当用户进入我的 NumericUpDown

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

    (请注意,Text属性隐藏在Intellisense中,但它确实存在)

        2
  •  8
  •   BrinkDaDrink    10 年前

    我想为未来一直在搜索Tab和Click的人添加此功能。

    Jon B的回答非常适合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;
            }
        }
    

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

        3
  •  4
  •   Kross    10 年前

    我环顾四周,我也遇到了同样的问题,这对我来说很有效,首先选择项目,第二个选择文本,希望这对将来有所帮助

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

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

    <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    11 年前

    我有多个数字盒子,我想为所有人实现这一点。我创建了:

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

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

        6
  •  -1
  •   Bugs Manjeet    8 年前

    对于通过鼠标单击或Tab键选择所有文本,我使用:

        public frmMain() {
            InitializeComponent();
            numericUpDown1.Enter += numericUpDown_SelectAll;
            numericUpDown1.MouseUp += numericUpDown_SelectAll;
        }
    
        private void numericUpDown_SelectAll(object sender, EventArgs e) {
            NumericUpDown box = sender as NumericUpDown;
            box.Select(0, box.Value.ToString().Length);
        }
    
    推荐文章