zoukankan      html  css  js  c++  java
  • [C#] 利用方向鍵移動 TextBox Focus

    論壇問題
    版面上有 100 個 textbox,編號為 1-100,textbox 排列為 1 欄 20 個,共 5 欄,當一開打這個 form 會將在第一欄第一列第一個 textbox 的背景顏色變更為黃色,然後可以透過鍵盤的 上、下、左、右來選取 textbox,當被選到的 textbox 背景顏色會為"黃色",沒有選到的會為灰色(預設)

    拉 100 個 TextBox 實在是太瘋狂了,只利用 10 個 TextBox、5 欄來練習
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    public partial class frmTextBoxFcous : Form
    {
        public frmTextBoxFcous()
        {
            InitializeComponent();
        }
     
        private void TextBoxFcous_Load(object sender, EventArgs e)
        {
            colorcontrol();
        }
     
        // override ProcessCmdKey 來進行鍵盤偵測
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // 偵測方向鍵:上、下、左、右
            if (keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Up || keyData == Keys.Down)
            {
                // 利用上、下、左、右來判斷,
                // 上 -1
                // 下 +1
                // 左 -2
                // 右 +2
                int count = 0;
                switch (keyData)
                {
                    case Keys.Up:
                        count--;
                        break;
                    case Keys.Down:
                        count++;
                        break;
                    case Keys.Left:
                        count -= 2;
                        break;
                    case Keys.Right:
                        count += 2;
                        break;
                }
     
                // 利用 ActiveControl 來判斷 Focus 現在在哪個控件上
                string ActiveControlName = this.ActiveControl.Name;
                // Form 上的 TextBox 控件名稱,故意用 TextBox1 - TextBox10 來命名,用名稱來控制 Focus 應該往哪跳
                int ActiveControlLength = ActiveControlName.Length;
                string NextControlName = "TextBox" + (Convert.ToInt16(ActiveControlName.Substring(7, ActiveControlLength - 7)) + count).ToString();
     
                // 確認 TextBox 是否真的存在
                Control[] ctls = this.Controls.Find(NextControlName, false);
                if (ctls.Length != 0) this.Controls[NextControlName].Focus();
            }
     
            colorcontrol();
            return base.ProcessCmdKey(ref msg, keyData);
        }
     
        private void colorcontrol()
        {
            // 不管三七二十一,先把全部的 TextBox 背景變成灰色
            // 此語法只針對 TextBox 去掃
            foreach (TextBox txt in this.Controls.OfType<textbox>())
            {
                txt.BackColor = Color.Gray;
            }
     
            // Form 一開始執行時就必須變色,此時無法偵測 ActiveControl,
            // 此情況顯示 TextBox1,同時也把 TextBox1 的 Tab Order 設為 0
            if (this.ActiveControl == null)
            {
                textBox1.BackColor = Color.Yellow;
            }
            else
            {
                this.ActiveControl.BackColor = Color.Yellow;
            }
        }
    }
    [C#] 利用方向鍵移動 TextBox Fcous
     
  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    107. Binary Tree Level Order Traversal II
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    104. Maximum Depth of Binary Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    系统和进程相关信息
    文件I/0缓冲
    系统编程概念(文件系统mount等函数的使用)
  • 原文地址:https://www.cnblogs.com/CCJVL/p/10894398.html
Copyright © 2011-2022 走看看