winform窗体中有一个pannel、一个button,
想 按回车时使pannel隐藏,但是 总是触发button事件怎么办啊,请大家给看看啊。
想 按回车时使pannel隐藏,但是 总是触发button事件怎么办啊,请大家给看看啊。
解决方案 »
- .net 一次执行多个oracle sql 语句
- C#中的引用类型
- vs2010 怎么添加用户控件?
- WinForm访问Web Service中的方法无效,在线等!!
- c#正则表达式1|323<>43|23<>23|45<>234|45
- Windows Workflow Foundation(3.0的) 问题,我怎么把wwf整和到别的程序中。
- Microsoft Enterprise Library 报错“Requested registry access is not allowed.”
- 关于DotNetSkin.dll和IrisSkin2.dll使用的问题
- 控件求助阿。。急啊。马上给分
- 如何将DataGrid表纵向排列
- C# 链接mysql数据库
- 如何给最小化的窗口 做快照
{
if (e.KeyCode == Keys.Enter)
{
if (panel2.Visible == true)
{
panel2.Visible = false;
}
else
{
try
{
//查询商品
查询本地商品 cxbdsp = new 查询本地商品(this);
cxbdsp.ShowDialog();
}
catch(Exception ex)
{
return;
}
}
}
}
其次,窗体的 AcceptButton 默认就是 null 的。无需设置。
第三,你按回车触发 Button 的 Click 事件是因为 Panel 无法获得焦点,Form 在遍历其子控件时发现只有 Button 能获得焦点,于是对焦于 Button,而回车事件恰巧又会触发 Click 事件。解决方案:使用 MouseClick 而不是 Click
原因:乍一看两者差不多,其实不然,前者只有在 Mouse 触发的情况下激活,Enter 无效,后者都会触发!
你可以使用体会一下。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} bool enterClickButton = false; private void button1_Click(object sender, EventArgs e)
{
if (enterClickButton)
{
MessageBox.Show("Entered!");
enterClickButton = false;
}
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Mouse Clicked!");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool b = base.ProcessCmdKey(ref msg, keyData); if (keyData == Keys.Enter)
{
enterClickButton = true;
} return b;
}
}