Question

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

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top