简体   繁体   English

如何设置选项卡按钮以在C#中的所有WinForms NumericUpDown中选择整个文本

[英]How to set the tab button to select the whole text in ALL the WinForms NumericUpDown in C#

My Requirement: 我的要求:

When someone presses the TAB button and moves to a NumericUpDown control in my form, the whole text of this to be selected, i searched a lot and i found this: 当有人按下TAB按钮并移到我的窗体中的NumericUpDown控件时,要选择的整个文本,我进行了很多搜索,然后发现:

private void numericUpDown1_Enter(object sender, EventArgs e)
    {
        numericUpDown1.Select(0, numericUpDown1.ToString().Length);
    }

I need some code that will do the job for ALL of them because my form has about 50 NumericUpDown controls I tried something like this: 我需要一些代码来完成所有任务,因为我的表单有大约50个NumericUpDown控件,我尝试过以下操作:

private void System.Windows.Forms.NumericUpDown_Enter(object sender, EventArgs e)
    {
        System.Windows.Forms.NumericUpDown.Select(0, 2);
    }

but two errors appeared: 但是出现了两个错误:

Error 2 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.UpDownBase.Select(int, int)' P:\\myWork\\C#\\sudoku\\sudoku\\Form1.cs 42 13 sudoku

Error 1 The modifier 'public' is not valid for this item P:\\myWork\\C#\\sudoku\\sudoku\\Form1.cs 40 21 sudoku

You need to access the instance of your NumericUpDown control being passed to the event through the sender parameter. 您需要访问通过sender参数传递给事件的NumericUpDown控件的实例。 Try this: (untested) 试试这个:( 未测试)

(Also, remove System.Windows.Forms from the beginning of the event name.) (此外,从事件名称的开头删除System.Windows.Forms 。)

private void NumericUpDown_Enter(object sender, EventArgs e)
{
    var numUpDownControl = sender as System.Windows.Forms.NumericUpDown;

    if (numUpDownControl != null)
        numUpDownControl.Select(0, 2);
}

If you want to select the entire value in the control (and not just the first two numbers), change the Select statement accordingly: 如果要选择控件中的整个值(而不仅仅是前两个数字),请相应地更改Select语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM