[英]How to select all text in Winforms NumericUpDown upon tab in?
[英]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.