简体   繁体   中英

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:

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:

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. Try this: (untested)

(Also, remove System.Windows.Forms from the beginning of the event name.)

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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