繁体   English   中英

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

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

我的要求:

当有人按下TAB按钮并移到我的窗体中的NumericUpDown控件时,要选择的整个文本,我进行了很多搜索,然后发现:

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

我需要一些代码来完成所有任务,因为我的表单有大约50个NumericUpDown控件,我尝试过以下操作:

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

但是出现了两个错误:

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

您需要访问通过sender参数传递给事件的NumericUpDown控件的实例。 试试这个:( 未测试)

(此外,从事件名称的开头删除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);
}

如果要选择控件中的整个值(而不仅仅是前两个数字),请相应地更改Select语句:

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

暂无
暂无

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

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