簡體   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