我想在 datagridview C# 中对单元格进行处理,当我按下单元格时,此特性会打开一个表单。
在 C# 中,没有一个事件(按键)允许我直接添加我的处理
经过网上搜索,我找到了以下解决方案
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)))
{
------
}
}
但它不起作用。
在调试中,执行了事件 dGridView_EditingControlShowing 的代码,但 Control_KeyPress 函数的代码未运行
有什么想法请
你应该设置你的Form
KeyPreview
财产给true。
并且您应该处理主窗体上的按键事件。
那是因为Control.KeyPress
event
按下某个键时发生当控件获得焦点时。 (msdn http://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.keypress.aspx)
public Form()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Control_KeyPress);
}
private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
//your code
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)