无法在C#Winforms Gridview Control中捕获TAB键 [英] Not able to trap the TAB key in C# Winforms Gridview Control

查看:115
本文介绍了无法在C#Winforms Gridview Control中捕获TAB键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专业人士,



经过彻底搜索后,我得出结论,在gridview上处理TAB键的最佳方法是覆盖 ProcessCmdKey( ref我在自定义类中的gridview控件的消息消息,键keyData)



这是我的自定义类。



  public   partial   class  GridViewForKeyPress:DataGridView 
{
public static Keys KeyPressed;

受保护 覆盖 bool ProcessCmdKey( ref 消息消息,键keyData)
{
KeyPressed = keyData;
return base .ProcessCmdKey( ref msg,keyData);
}
}





我已将此控件添加到包含GridViewForKeyPress控件的表单中。在我的控制事件代码表格中:



 私人 < span class =code-keyword> void  gvOutletRates_EditingControlShowing( object  sender,DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtrate = e。将控制为 TextBox;
txtrate.KeyDown - = txtrate_KeyDown;
txtrate.KeyDown + = txtrate_KeyDown;
}

void txtrate_KeyDown( object sender,KeyEventArgs e)
{
MessageBox.Show(MyCustomControls.GridViewForKeyPress.KeyPressed.ToString());
}







按键,



对于字母数字键,Backspace,Space等,MessageBox显示KeyCode。



当按下Enter键时,No Message但控制移动到下一个单元格。



当Tab键时按下,选择单元格。没有消息。



我哪里错了,或逻辑完全错了?如何在网格视图上正确捕获TAB键。请建议。



提前致谢。我是学习者。请原谅任何不熟练的逻辑程序。

解决方案

表单正在捕获TAB键。这就是Windows窗体通常如何将焦点更改为Form的Tab键顺序中的下一个控件。



所以......,你必须改变WinForm对象的方式( DataGridView)处理TAB键:

  private   bool  isTabCaptureOn =  false ; 
受保护 覆盖 bool ProcessTabKey ( bool forward)
{
if (isTabCaptureOn)
{
return true ;
}
其他
{
返回 .ProcessTabKey(正向);
}
}





当isTabCaptureOn为true时,表单将允许您分配和使用DataGridView的 KeyUp()以您正在寻找的方式处理TAB密钥的方法。但是,除非你开发一种方法再次关闭捕获,否则你将无法将控制器移出另一个控件。



我将把这个练习留给你....


Dear Professionals,

After making a thorough search, I came to conclusion that best way to handle TAB key on gridview was to override the ProcessCmdKey(ref Message msg, Keys keyData) of the gridview control in my custom class.

This is my custom class.

public partial class GridViewForKeyPress : DataGridView
{
    public static Keys KeyPressed;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        KeyPressed = keyData;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}



I have added this control to my form which contains my GridViewForKeyPress Control. In the form my code on the control's events :

private void gvOutletRates_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox txtrate = e.Control as TextBox;
    txtrate.KeyDown -= txtrate_KeyDown;
    txtrate.KeyDown += txtrate_KeyDown;
}

void txtrate_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show(MyCustomControls.GridViewForKeyPress.KeyPressed.ToString());
}




On a key press,

For alphanumeric keys, Backspace, Space etc., MessageBox shows the KeyCode.

When Enter Key is pressed, No Message but control moves to next cell.

When Tab key is pressed, the cell is selected. No Message.

Where am I going wrong, or is the logic totally wrong ? How to correctly trap the TAB key on the Grid View. Please suggest.

Thanks in advance. I am a learner. Please excuse any unskilled logic procedures.

解决方案

The Form is capturing the TAB key. That is how a Windows form normally works to change focus to the next control within the Form's tab order.

So..., you have to change how the WinForm object ( not the DataGridView ) handle the TAB key:

private bool isTabCaptureOn = false;
protected override bool ProcessTabKey(bool forward)
{
    if (isTabCaptureOn)
    {
        return true;
    }
    else
    {
        return base.ProcessTabKey(forward);
    }
}



When isTabCaptureOn is true, the form will allow you to assign and work with the DataGridView's KeyUp() method to process the TAB key in the fashion that you are seeking. However, you will not be able to TAB out of the control to another control unless you develop a method to turn capture off again.

I'll leave that exercise to you....


这篇关于无法在C#Winforms Gridview Control中捕获TAB键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆