winform窗体无法捕捉keypress事件!

shelmesky 2009-09-14 08:31:10
各位大大,小弟目前正学习C#中,在处理textbox的事件中遇到问题,麻烦哪位高手有空指导一下小弟。
下面是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace csharp_program
{

public partial class inputbox : Form
{
public inputbox()
{
InitializeComponent();
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < 48 || e.KeyChar > 57 || e.KeyChar == 13)
{
MessageBox.Show("you press Enter!");
}
}

}
}



问题是,在textbox中输入完毕后,按回车键没有任何反应,下断点进入F5调试,也无法执行相关代码。
...全文
305 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shelmesky 2009-09-14
  • 打赏
  • 举报
回复
不好意思,分数有限,高手又这么多,小弟无法保证每位都有,所以只好先到先得了。
wuyq11 2009-09-14
  • 打赏
  • 举报
回复
没问题,事件是否丢失
LutzMark 2009-09-14
  • 打赏
  • 举报
回复
相应控件的事件委托没有加吧
shelmesky 2009-09-14
  • 打赏
  • 举报
回复
回 lxcnn :找到问题所在了,this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
不存在,自己又添加了进去,问题解决!多谢!
limeng315 2009-09-14
  • 打赏
  • 举报
回复
检查一下你的inputbox.designer.cs中有没有类似this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
sito_hongta 2009-09-14
  • 打赏
  • 举报
回复
那應該是沒有註冊該事件了,打開designer文字看看有沒有註冊~~
-过客- 2009-09-14
  • 打赏
  • 举报
回复
看下Form的Designer.cs中
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
是否丢失
shelmesky 2009-09-14
  • 打赏
  • 举报
回复
回crecrecre: 下面捕捉keydown事件的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace csharp_program
{

public partial class inputbox : Form
{
public inputbox()
{
InitializeComponent();
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
MessageBox.Show("you press Enter!");
}
}

}
}

问题还是存在,哎。。。
shelmesky 2009-09-14
  • 打赏
  • 举报
回复
回liffe:去掉if判断还是没有捕捉到keypress事件,也没有执行到代码。
crecrecre 2009-09-14
  • 打赏
  • 举报
回复
最好用keydown事件
liffe 2009-09-14
  • 打赏
  • 举报
回复
你焦点是不是在textbox里面?先去掉if判断,看看有没有变化?

110,555

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧