C# 擷取鍵盤輸入數位值

來源:互聯網
上載者:User

  /// <summary>
        /// 重載ProcessCmdKey事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            int index = dgvBasesList.CurrentCell.RowIndex;
            string text = Clipboard.GetText();
            EventArgs e = new EventArgs();
            if (keyData == Keys.Enter)
            {
                this.btnOK_Click(this, e);
            }
            else if (keyData == Keys.Down)
            {
                if (index < dgvBasesList.Rows.Count - 1)
                {
                    // dgvBasesList.Rows[index + 1].Selected =true ;
                    dgvBasesList.CurrentCell = dgvBasesList[0, index + 1];
                    txtNo.Text = (dgvBasesList.CurrentRow.Index + 1).ToString();
                }
            }
            else if (keyData == Keys.Up)
            {
                if (index > 0)
                {
                    dgvBasesList.CurrentCell = dgvBasesList[0, index - 1];
                    txtNo.Text = (dgvBasesList.CurrentRow.Index + 1).ToString();
                }
            }
            else if ((keyData >= Keys.D0 && keyData <= Keys.D9) || (keyData >= Keys.NumPad0 && keyData <= Keys.NumPad9))
            {
                string num = KeyCodeToStr(keyData);
                string num1 = num;
                int no = Int32.Parse(txtNo.Text.ToString() + num);
                if (no > dgvBasesList.Rows.Count - 1)
                {
                    if (Int32.Parse(num) > dgvBasesList.Rows.Count)
                    {
                        dgvBasesList.CurrentCell = dgvBasesList[0, dgvBasesList.Rows.Count - 1];
                        txtNo.Text = (dgvBasesList.Rows.Count - 1).ToString();
                    }
                    else if (Int32.Parse(num) > 0)
                    {
                        dgvBasesList.CurrentCell = dgvBasesList[0, Int32.Parse(num) - 1];
                        txtNo.Text = num.ToString();
                    }
                }
                else
                {
                    dgvBasesList.CurrentCell = dgvBasesList[0, no - 1];
                    txtNo.Text = no.ToString();
                }
            }
            return true;

        }
        [DllImport("user32.dll")]
        static extern int MapVirtualKey(uint uCode, uint uMapType);

        private static char KeyCodeToChar(Keys k)
        {
            int nonVirtualKey = MapVirtualKey((uint)k, 2);
            char mappedChar = Convert.ToChar(nonVirtualKey);
            return mappedChar;
        }
        public static string KeyCodeToStr(Keys k)
        {
            char mappedChar = KeyCodeToChar(k);
            string str = mappedChar.ToString();
            if (Char.IsWhiteSpace(mappedChar) || string.IsNullOrEmpty(str) || mappedChar == '\r' || mappedChar == '\n' || mappedChar == KeyCodeToChar(Keys.F1))
            {
                return k.ToString();
            }
            else
            {
                return str + "";
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.