textbox回车跳转到button

xzf_fancy 2012-05-22 06:20:32
我有多个textbox 和多个button,如何控制我的textbox回车会执行对应的button。

例如:textbox1对应button1。

应该怎么写?
...全文
220 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
EdsionWang 2012-05-23
  • 打赏
  • 举报
回复
相应的textBox的KeyPress事件注册到对应的button_click事件
[Quote=引用楼主 的回复:]
我有多个textbox 和多个button,如何控制我的textbox回车会执行对应的button。

例如:textbox1对应button1。

应该怎么写?
[/Quote]
theillusion 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]



上面的是不行的,,,那个是针对窗体的,,不是针对对应textbox的,,

[/Quote]




private void textBox1_Enter(object sender, EventArgs e)
{
this.AcceptButton = button1;
}

private void textBox1_Leave(object sender, EventArgs e)
{
this.AcceptButton = null;
}

private void textBox2_Enter(object sender, EventArgs e)
{
this.AcceptButton = button2;
}

private void textBox2_Leave(object sender, EventArgs e)
{
this.AcceptButton = null;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}


代码有点乱,楼主自己整理
熙风 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 2 楼 的回复:

textbox 获得焦点时,设置对应的 button 为窗体的 AcceptButton


代码怎么写?
[/Quote]

上面的是不行的,,,那个是针对窗体的,,不是针对对应textbox的,,

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
button1_Click(null, null);
}

private void button1_Click(object sender, EventArgs e)
{
}
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

textbox 获得焦点时,设置对应的 button 为窗体的 AcceptButton
[/Quote]

代码怎么写?
ONE-PIECE 2012-05-23
  • 打赏
  • 举报
回复
这个可以 用JS

[Quote=引用 1 楼 的回复:]
this.button1.Focus();
可以注册到同一个keypress事件里面,统一处理
[/Quote]
  • 打赏
  • 举报
回复
private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (textbox.Text != "" && e.KeyChar == (char)13) { 按钮名称_Click(sender, e); }
}
cnfixit 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
textbox 获得焦点时,设置对应的 button 为窗体的 AcceptButton
[/Quote]
这个方法好
theillusion 2012-05-22
  • 打赏
  • 举报
回复
textbox 获得焦点时,设置对应的 button 为窗体的 AcceptButton
cnfixit 2012-05-22
  • 打赏
  • 举报
回复
this.button1.Focus();
可以注册到同一个keypress事件里面,统一处理

111,126

社区成员

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

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

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