DataGridView中,如何捕捉一个细胞的KeyPress事件C# [英] DataGridView, how to capture a cell's KeyPress event C#

查看:875
本文介绍了DataGridView中,如何捕捉一个细胞的KeyPress事件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview的C#中的细胞做treatement,当我按下一个单元格这TRAITEMENT是开放的一种形式。



在C#有没有一个事件(按键),让我直接加我的治疗



互联网上搜索后,我发现了以下解决方案

 私人无效dGridView_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E)
{
e.Control.KeyPress + =
新KeyPressEventHandler(Control_KeyPress);
}


私人无效Control_KeyPress(对象发件人,KeyPressEventArgs E)
{
如果((Strings.Asc(e.KeyChar)> = Strings.Asc(Keys.A.ToString())及&放大器; Strings.Asc(e.KeyChar)GT = Strings.Asc(Keys.Z.ToString()))||(Strings.Asc(e.KeyChar )> = Strings.Asc(Keys.D0.ToString())及&放大器; Strings.Asc(e.KeyChar)GT = Strings.Asc(Keys.D9.ToString())||(Strings.Asc( e.KeyChar)> = 97安培;&安培; Strings.Asc(e.KeyChar)GT 122)))
{
------
}
}

,但它不工作。
在调试dGridView_EditingControlShowing执行该事件的代码,但Control_KeyPress功能的代码不能运行。



任何想法,请


< DIV CLASS =h2_lin>解决方案

您应该设置你的表格 的KeyPreview proprety到的真正的。
你应该处理主窗体上按下的键事件。
这是因为 Control.KeyPress 事件




时发生的关键按下而控件有焦点。 MSDN




 公开表()
{
的InitializeComponent();
this.KeyPreview = TRUE;
this.KeyPress + =新KeyPressEventHandler(Control_KeyPress);
}

私人无效Control_KeyPress(对象发件人,KeyPressEventArgs E)
{
//你的代码
}


i want to do treatement for a cell in datagridview c#, this traitement is open a form when i press a cell.

in C# there isn't an event (keypress) which allows me to add my treatment directly

After search on internet, I found the following solution

 private void dGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress +=
        new KeyPressEventHandler(Control_KeyPress);
    }


           private void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.A.ToString()) && Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.Z.ToString())) || (Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.D0.ToString()) && Strings.Asc(e.KeyChar) >= Strings.Asc(Keys.D9.ToString()) || (Strings.Asc(e.KeyChar) >= 97 && Strings.Asc(e.KeyChar) > 122)))
            {
                ------
            }
      }

but it doesn't work. in debug the code of the event dGridView_EditingControlShowing was executed but the code of Control_KeyPress function does not run

any ideas please

解决方案

You should set your Form KeyPreview proprety to true. And you should handle the key pressed event on the main form. That is because Control.KeyPress event

Occurs when a key is pressed while the control has focus. (msdn)

public Form()
{
    InitializeComponent();
    this.KeyPreview = true;
    this.KeyPress += new KeyPressEventHandler(Control_KeyPress);
}

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    //your code
}

这篇关于DataGridView中,如何捕捉一个细胞的KeyPress事件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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