按键监听及重写

一、PreviewKeyDown、KeyDown、KeyPress、KeyUp相关:http://blog.csdn.net/jiankunking/article/details/20611383

需要焦点在控件上才能监听按键。

二、以下代码是重写事件来监听左右键,取消点击回车在datagridview中选中下一行。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                if (tabControl1.SelectedIndex == 0)
                    showCountP();
                else if (tabControl1.SelectedIndex == 1)
                    showCountI();

                return base.ProcessDialogKey(keyData);
            }
            if (keyData == Keys.Left)
            {
                if (tabControl1.SelectedIndex == 0)
                    tabControl1.SelectedTab = tabPage2;
                else if (tabControl1.SelectedIndex == 1)
                    tabControl1.SelectedTab = tabPage1;
                return true;
            }
            if (keyData == Keys.Right)
            {
                if (tabControl1.SelectedIndex == 0)
                    tabControl1.SelectedTab = tabPage2;
                else if (tabControl1.SelectedIndex == 1)
                    tabControl1.SelectedTab = tabPage1;
                return true;
            }
            else
            {
                return false;
            }

        }

 

posted @ 2016-07-30 15:06  蓝瑟黄昏  阅读(341)  评论(0编辑  收藏  举报