输入按钮移动到下一个控件 [英] Enter Button To Move To Next Control

查看:88
本文介绍了输入按钮移动到下一个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows窗体中为文本框启用输入按钮移动到下一个控件而不是使用C#?

解决方案

  private   void  TextBox_KeyPress( object  sender,KeyPressEventArgs e)
{
if (e.KeyChar == ' \ r'
{
if this .ActiveControl!= null
{
this .SelectNextControl( this .ActiveControl, true true true true );
}
e.Handled = true ; // 将事件标记为已处理
}
}


一种可能的方法是处理 KeyPress [ ^ ]事件,检查ENTER键并将焦点设置为下一个控件。

但是对于你的情况,我更喜欢创建一个继承<$的用户控件c $ c> TextBox 并覆盖 KeyPress 方法。

如果用户按下ENTER,你可以调用 Control.SelectNextControl() SendKeys.Send({TAB} )。 :)


在ide中选择表单并启用键预览为真。

 私人  Sub  myEventHandler( ByVal  sender  As  对象 ByVal  e 作为 System.Windows.Forms.KeyEventArgs)句柄  .KeyDown 
如果 e.KeyCode = Keys.Enter 那么

e.SuppressKeyPress = True
.SelectNextControl( Me .ActiveControl, True True True
结束 如果
结束 Sub



 私有  Sub  addcustomer_Load( ByVal  sender 作为 对象 ByVal  e  As  System.EventArgs)句柄  .Load 
Dim ctl 作为控制
对于 每个 ctl .Controls
AddHandler ctl.KeyDown, AddressOf myEventHandler
下一步
结束


How to Enable Enter Button To Move To Next Control Instead to Tab using C# in Windows Forms for a Textbox ?

解决方案

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == '\r')
     {
	if (this.ActiveControl != null)
        {
            this.SelectNextControl(this.ActiveControl, true, true, true, true);
        }
	e.Handled = true; // Mark the event as handled
     }
}


One possible way is to handle the KeyPress[^] event, check for ENTER key and set the focus to the next control.
But for your case I'd prefer creating a user control which inherits TextBox and overrides the KeyPress method.
If the user presses ENTER you can call Control.SelectNextControl() or SendKeys.Send("{TAB}"). :)


Select form in ide and enable key preview to true.

Private Sub myEventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      If e.KeyCode = Keys.Enter Then

          e.SuppressKeyPress = True
          Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
      End If
  End Sub


 Private Sub addcustomer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ctl As Control
        For Each ctl In Me.Controls
            AddHandler ctl.KeyDown, AddressOf myEventHandler
        Next 
End Sub


这篇关于输入按钮移动到下一个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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