你得扑捉回车事件,好像在属性里没有简单的选一下false或ture这样的选项

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3434/3434227.xml?temp=.8916742
    增加了一个属性,决定回车后光标的走向,可以是上下左右四个方向以及根据上一方向自动决定方向五个选择
      

  2.   

    datagrid單元格好像不支持鍵盤事件
      

  3.   

    Set Up the Key Trap
    To trap keystrokes in a Windows Forms control, you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey method. In this overridden method, you will place the code to process the keystrokes that you want to trap. The following sample code is an example of the basic structure for such a class: 
    class MyDataGrid : System.Windows.Forms.DataGrid
    {
       protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
       }
    }  
    back to the top Implement the Overridden Method
    The system passes two parameters to the ProcessCmdKey method: msg and keyData. The msg parameter contains the Windows Message, such as WM_KEYDOWN. The keyData parameter contains the key code of the key that was pressed. If CTRL or ALT was also pressed, the keyData parameter contains the ModifierKey information.Using the msg parameter is not mandatory; you can ignore it. It is good practice, however, to test the message. In this example, you test WM_KEYDOWN to verify that this is a keystroke event. You also test WM_SYSKEYDOWN, so that it is possible to catch keystroke combinations that include control keys (primarily ALT and CTRL).To trap specific keys, you can evaluate the keyCode by comparing it to the Keys enumeration. The following code sample demonstrates how to catch the keystrokes UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z: 
    const int WM_KEYDOWN = 0x100;
    const int WM_SYSKEYDOWN = 0x104;
       
    if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
    {
       switch(keyData)
          {
             case Keys.Down:
                Console.WriteLine("Down Arrow Captured");
                break;
          
             case Keys.Up:
                Console.WriteLine("Up Arrow Captured");
                break;
     
             case Keys.Tab:
                Console.WriteLine("Tab Key Captured");
                break;
     
             case Keys.Control | Keys.M:
                Console.WriteLine("<CTRL> + m Captured");
                break;
     
             case Keys.Alt | Keys.Z:
                Console.WriteLine("<ALT> + z Captured");
                break;
          }

    back to the top Build an Example
    The following example shows how to trap keystrokes with the DataGrid control. 
    Create a new Windows Control Library project in Visual C# .NET. 
    View the properties for the class UserControl1, and then change the name to MyDataGrid. 
    View the code for the Control Library, and then change the following line of code 
    public class MyDataGrid : System.Windows.Forms.UserControl 
    to the following: 
    public class MyDataGrid : System.Windows.Forms.DataGrid 
    Add the following method to the MyDataGrid class: 
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
       const int WM_KEYDOWN = 0x100;
       const int WM_SYSKEYDOWN = 0x104;   if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
       {
          switch(keyData)
          {
             case Keys.Down:
                this.Parent.Text="Down Arrow Captured";
                break;
          
             case Keys.Up:
                this.Parent.Text="Up Arrow Captured";
                break;
     
             case Keys.Tab:
                this.Parent.Text="Tab Key Captured";
                break;
     
             case Keys.Control | Keys.M:
                this.Parent.Text="<CTRL> + M Captured";
                break;
     
             case Keys.Alt | Keys.Z:
                this.Parent.Text="<ALT> + Z Captured";
                break;
          }
       }   return base.ProcessCmdKey(ref msg,keyData);

    Build the project. 
    Create a new Windows Application project in Visual C# .NET. By default, Form1 is created. 
    On the Tools menu, click Customize Toolbox. 
    Click the .NET Framework Components tab. 
    Click Browse, find the control/DLL that was just created, and then click OK. 
    The control MyDataGrid now appears in the toolbox. Place one on Form1. NOTE: You can use the code in the remaining steps to create sample data for the grid to display.
    Add the following code to the namespace of the form. You can place the code either before or after the Form Class definition. 
    // This structure is only used in providing sample data for the grid.
    public struct gridData
    {
       private string make;
       private int year;   public gridData(string n,int y)
       {
          make=n;
          year=y;
       }   public string Make
       {
          get{return make;}
          set{make = value;}
       }   public int Year
       {
          get{return year;}
          set{year=value;}
       }

    Add the following code to the form class, immediately following the "Windows Form Designer generated code" section: 
    protected gridData[] dataArray=new gridData[5]; 
    Add the following code to the Load event of Form1: 
    // Create some sample data.
    dataArray[0]=new gridData("ford",1999);
    dataArray[1]=new gridData("chevrolet",1999);
    dataArray[2]=new gridData("plymouth",1988);
    dataArray[3]=new gridData("honda",1999);
    dataArray[4]=new gridData("fiat",1987);// Assign the data to the grid.
    myDataGrid1.DataSource=dataArray; 
    Run the sample, and try the various keystrokes that are being trapped (UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z). The caption of the form is updated to show which keystroke was pressed. 看看这篇吧,应该能解决你的问题。
      

  4.   

    上面这篇文章是在VS.net2003的帮助中找到的。ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/enu_kbvcsharpnetkb/en-us/vcsharpnetkb/Q320584.htm
    这儿还有一个在网上找的。不知地址了。
    对于Datagrid的键盘事件响应,我们一定要分辨清楚是Datagrid自身的,还是cell单元格的键盘事件响应,处理方法是有很大区别的。大家可以参考第一篇的方法,对cell中的键盘事件进行处理。    但有个问题上一节没有交待,就是如何对Enter键、方向键、Tab键、Pgup/PgDn这些虚键进行拦截与处理?一般网友遇到这个问题,十有八九是出于这个目的:就是希望按Enter键时使光标在一行中向右一个格一个格跳(从“name”到“123”),而不是立即跳向同一列的下一行(到“xxx”)。
    但是,我们在上一篇中用尽方法也截不下Enter键呀,看来这一键盘响应已经被控件封装为protected型了。控件编写人员把多个预定义的键盘绑定方案封装起来,称为快捷键。我们在Keydown/KeyPress中没办法拦截到Enter键和其它一些键盘按键正是这个原因。    快捷键与菜单快捷方式被称为命令键,应用程序会在对常规输入进行处理前的消息预处理过程中对它们进行处理。命令键也就始终比常规输入键具有优先权。    ProcessCmdKey 方法首先确定控件是否有上下文菜单,如果有,则允许 ContextMenu 处理命令键。如果命令键不是菜单快捷方式,且控件有父级,那么该键传递到父级的 ProcessCmdKey 方法。净效果是命令键在控件层次结构中向上“冒”。除了用户按下的键外,键数据还指示哪些(如果有的话)修改键与该键同时按下。修改键包括 SHIFT、CTRL 和 ALT 键(成为组合键)。    这里要注意:该方法必须返回 true,以指示它已经处理完命令键,或者 false,以指示该键不是命令键。在派生类中重写 ProcessCmdKey 方法时,控件应返回 true 以指示它已处理该键。对于未由该控件处理的键,应返回调用基类的 ProcessCmdKey 方法的结果。    如果不加返回值,会默认为false。这样你明明已经修改了处理方法,却会在执行完你的命令之后,继续执行父类中定义的该键盘按键的处理方法。    那么我们怎么处理文头的命题?解决之道就是自己写一个控件,继承自现有的Datagrid控件,再重写处理命令键响应程序ProcessCmdKey,来实现我们的需求。    步骤一:在vs.net编辑器中,“文件”->“新建”->“项目”,然后选择新建一个“Windows控件库”的项目:HenryDatagrid。这样运行的结果会生成一个DLL文件,而不是EXE执行文件;    步骤二:在HenryDatagrid.vb文件代码编辑窗口中加入有阴影的这句话:
    Public Class HenryDatagridInherits System.Windows.Forms.DataGrid   ‘这表示新建的控件是Datagrid的派生控件
    步骤三:在“类名”窗口中选择overrides,然后在“方法名称”窗口选择“ProcessCmdKey”
    然后就会出现一段空的ProcessCmdKey代码段,我们可以写入自己的代码:
    Protected Overrides Function ProcessCmdKey
    (ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean        Dim WM_KEYDOWN As Integer = 256  ‘消息响应的问题可以参考其他win32编程的文章        Dim WM_SYSKEYDOWN As Integer = 260         If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then            Select Case keyData                Case Keys.Down                       MsgBox("截到下箭头键")                    Return True                Case Keys.Up                    MsgBox("截到上箭头键")                    Return True                Case Keys.Enter                    SendKeys.Send("{Tab}")                    Return True                Case Keys.Control + Keys.M                    MsgBox("<CTRL> + m 组合键被截获")                    Return True                Case Keys.Alt + Keys.Z                    MsgBox("<ALT> + z 组合键被截获")                    Return True            End Select        End If End Function
    然后运行一下,生成HenryDatagrid.dll文件    步骤四:再建立一个项目,然后在新项目的设计窗口的工具箱上单击鼠标右键,在弹出菜单中选择“添加引用”,然后在.net选项卡中占击“浏览”,选择到HenryDatagrid.dll,加入进来,然后您的工具箱上会多出一个HenryDatagrid的图标,在新项目中使用HenryDatagrid来代替datagrid控件。看一下,您所需要的“Enter跳格”事件就这样完成了。    建议:您在Keys.Enter代码中的Return True去掉,看一下会有什么情况发生。    这里说句题外话,我们在重写类方法时,必须也只能使用Overridable关键字修饰的Protected     方法。这是因为在VB中是用Overridable 关键字指定属性或方法可以在派生类中重写。没有这个东东的我们也没有资格重写了。
      

  5.   

    在datagrid里面截获不到回车,但是你可以在整个窗体里面截获然后自己写
                      /// <summary>
    /// 截获回车事件
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) 

    Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode; 
                
    if((keyCode ==Keys.Enter)&&(this.ActiveControl is System.Windows.Forms.DataGridTextBox ))
    {
    //你的代码
    return true;
    }
    return false;
    }
      

  6.   

    CSTerry(Terry) :问题是焦点在datagrid中的单元格中会响应你上面代码吗?
      

  7.   

    焦点在datagrid中的单元格中时好像其它地方也截获不到键盘事件,这是真的吗?
      

  8.   

    据我所知,用Ctrl+Enter可以在单元格里换行。
      

  9.   

    焦点在datagrid中的单元格中时好像其它地方也截获不到键盘事件,这是真的吗?
      

  10.   

    wq_sc(陶强) 分给你了 给我程序吧