让Enter键实现Tab键的功能

开发环境:VS.NET 2005, .net framework 2.0;
开发语言: C# 2.0

窗体设计器生成的代码(Form1.Designer.cs)

  1. namespace  MyTest
  2. {
  3.     partial  class  Form1
  4.     {
  5.          /// <summary>
  6.          /// 必需的设计器变量。
  7.          /// </summary>
  8.          private  System.ComponentModel.IContainer components =  null ;
  9.          /// <summary>
  10.          /// 清理所有正在使用的资源。
  11.          /// </summary>
  12.          /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.          protected   override   void  Dispose( bool  disposing)
  14.         {
  15.              if  (disposing && (components !=  null ))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.              base .Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.          /// <summary>
  23.          /// 设计器支持所需的方法 - 不要
  24.          /// 使用代码编辑器修改此方法的内容。
  25.          /// </summary>
  26.          private   void  InitializeComponent()
  27.         {
  28.              this .groupBox1 =  new  System.Windows.Forms.GroupBox();
  29.              this .cboBoxFirst =  new  System.Windows.Forms.ComboBox();
  30.              this .button1 =  new  System.Windows.Forms.Button();
  31.              this .txtBox =  new  System.Windows.Forms.TextBox();
  32.              this .groupBox2 =  new  System.Windows.Forms.GroupBox();
  33.              this .button2 =  new  System.Windows.Forms.Button();
  34.              this .txtLast =  new  System.Windows.Forms.TextBox();
  35.              this .groupBox3 =  new  System.Windows.Forms.GroupBox();
  36.              this .radioButton2 =  new  System.Windows.Forms.RadioButton();
  37.              this .radioButton1 =  new  System.Windows.Forms.RadioButton();
  38.              this .groupBox1.SuspendLayout();
  39.              this .groupBox2.SuspendLayout();
  40.              this .groupBox3.SuspendLayout();
  41.              this .SuspendLayout();
  42.              // 
  43.              // groupBox1
  44.              // 
  45.              this .groupBox1.Controls.Add( this .cboBoxFirst);
  46.              this .groupBox1.Controls.Add( this .button1);
  47.              this .groupBox1.Controls.Add( this .txtBox);
  48.              this .groupBox1.Location =  new  System.Drawing.Point(23, 12);
  49.              this .groupBox1.Name =  "groupBox1" ;
  50.              this .groupBox1.Size =  new  System.Drawing.Size(256, 159);
  51.              this .groupBox1.TabIndex = 1;
  52.              this .groupBox1.TabStop =  false ;
  53.              this .groupBox1.Text =  "groupBox1" ;
  54.              // 
  55.              // cboBoxFirst
  56.              // 
  57.              this .cboBoxFirst.FormattingEnabled =  true ;
  58.              this .cboBoxFirst.Items.AddRange( new   object [] {
  59.              "小明" ,
  60.              "校长" ,
  61.              "主任" });
  62.              this .cboBoxFirst.Location =  new  System.Drawing.Point(7, 33);
  63.              this .cboBoxFirst.Name =  "cboBoxFirst" ;
  64.              this .cboBoxFirst.Size =  new  System.Drawing.Size(121, 20);
  65.              this .cboBoxFirst.TabIndex = 2;
  66.              // 
  67.              // button1
  68.              // 
  69.              this .button1.AccessibleName =  "SKIP" ;
  70.              this .button1.Location =  new  System.Drawing.Point(122, 85);
  71.              this .button1.Name =  "button1" ;
  72.              this .button1.Size =  new  System.Drawing.Size(52, 23);
  73.              this .button1.TabIndex = 8;
  74.              this .button1.Text =  "button1" ;
  75.              this .button1.UseVisualStyleBackColor =  true ;
  76.              // 
  77.              // txtBox
  78.              // 
  79.              this .txtBox.Location =  new  System.Drawing.Point(6, 87);
  80.              this .txtBox.Name =  "txtBox" ;
  81.              this .txtBox.Size =  new  System.Drawing.Size(100, 19);
  82.              this .txtBox.TabIndex = 4;
  83.              // 
  84.              // groupBox2
  85.              // 
  86.              this .groupBox2.Controls.Add( this .button2);
  87.              this .groupBox2.Controls.Add( this .txtLast);
  88.              this .groupBox2.Location =  new  System.Drawing.Point(23, 259);
  89.              this .groupBox2.Name =  "groupBox2" ;
  90.              this .groupBox2.Size =  new  System.Drawing.Size(256, 100);
  91.              this .groupBox2.TabIndex = 28;
  92.              this .groupBox2.TabStop =  false ;
  93.              this .groupBox2.Text =  "groupBox2" ;
  94.              // 
  95.              // button2
  96.              // 
  97.              this .button2.AccessibleName =  "SKIP" ;
  98.              this .button2.Location =  new  System.Drawing.Point(122, 42);
  99.              this .button2.Name =  "button2" ;
  100.              this .button2.Size =  new  System.Drawing.Size(60, 23);
  101.              this .button2.TabIndex = 36;
  102.              this .button2.Text =  "button2" ;
  103.              this .button2.UseVisualStyleBackColor =  true ;
  104.              // 
  105.              // txtLast
  106.              // 
  107.              this .txtLast.Location =  new  System.Drawing.Point(7, 46);
  108.              this .txtLast.Name =  "txtLast" ;
  109.              this .txtLast.Size =  new  System.Drawing.Size(100, 19);
  110.              this .txtLast.TabIndex = 32;
  111.              // 
  112.              // groupBox3
  113.              // 
  114.              this .groupBox3.Controls.Add( this .radioButton2);
  115.              this .groupBox3.Controls.Add( this .radioButton1);
  116.              this .groupBox3.Location =  new  System.Drawing.Point(23, 177);
  117.              this .groupBox3.Name =  "groupBox3" ;
  118.              this .groupBox3.Size =  new  System.Drawing.Size(256, 65);
  119.              this .groupBox3.TabIndex = 24;
  120.              this .groupBox3.TabStop =  false ;
  121.              this .groupBox3.Text =  "groupBox3" ;
  122.              // 
  123.              // radioButton2
  124.              // 
  125.              this .radioButton2.AutoSize =  true ;
  126.              this .radioButton2.Location =  new  System.Drawing.Point(110, 29);
  127.              this .radioButton2.Name =  "radioButton2" ;
  128.              this .radioButton2.Size =  new  System.Drawing.Size(88, 16);
  129.              this .radioButton2.TabIndex = 32;
  130.              this .radioButton2.TabStop =  true ;
  131.              this .radioButton2.Text =  "radioButton2" ;
  132.              this .radioButton2.UseVisualStyleBackColor =  true ;
  133.              // 
  134.              // radioButton1
  135.              // 
  136.              this .radioButton1.AutoSize =  true ;
  137.              this .radioButton1.Location =  new  System.Drawing.Point(6, 29);
  138.              this .radioButton1.Name =  "radioButton1" ;
  139.              this .radioButton1.Size =  new  System.Drawing.Size(88, 16);
  140.              this .radioButton1.TabIndex = 28;
  141.              this .radioButton1.TabStop =  true ;
  142.              this .radioButton1.Text =  "radioButton1" ;
  143.              this .radioButton1.UseVisualStyleBackColor =  true ;
  144.              // 
  145.              // Form1
  146.              // 
  147.              this .AutoScaleDimensions =  new  System.Drawing.SizeF(6F, 12F);
  148.              this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  149.              this .ClientSize =  new  System.Drawing.Size(524, 380);
  150.              this .Controls.Add( this .groupBox3);
  151.              this .Controls.Add( this .groupBox2);
  152.              this .Controls.Add( this .groupBox1);
  153.              this .Name =  "Form1" ;
  154.              this .Text =  "Form1" ;
  155.              this .Load +=  new  System.EventHandler( this .Form1_Load);
  156.              this .groupBox1.ResumeLayout( false );
  157.              this .groupBox1.PerformLayout();
  158.              this .groupBox2.ResumeLayout( false );
  159.              this .groupBox2.PerformLayout();
  160.              this .groupBox3.ResumeLayout( false );
  161.              this .groupBox3.PerformLayout();
  162.              this .ResumeLayout( false );
  163.         }
  164.         #endregion
  165.          private  System.Windows.Forms.GroupBox groupBox1;
  166.          private  System.Windows.Forms.Button button1;
  167.          private  System.Windows.Forms.TextBox txtBox;
  168.          private  System.Windows.Forms.GroupBox groupBox2;
  169.          private  System.Windows.Forms.Button button2;
  170.          private  System.Windows.Forms.TextBox txtLast;
  171.          private  System.Windows.Forms.ComboBox cboBoxFirst;
  172.          private  System.Windows.Forms.GroupBox groupBox3;
  173.          private  System.Windows.Forms.RadioButton radioButton2;
  174.          private  System.Windows.Forms.RadioButton radioButton1;
  175.     }
  176. }


Form关联文件(功能实现代码):

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.ComponentModel;
  4. using  System.Data;
  5. using  System.Drawing;
  6. using  System.Text;
  7. using  System.Windows.Forms;
  8. namespace  MyTest
  9. {
  10.      public  partial  class  Form1 : Form
  11.     {
  12.         #region"页面控件初始化"
  13.          public  Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         #endregion
  18.         #region"页面初始化(Form_Load)"
  19.          private   void  Form1_Load( object  sender, EventArgs e)
  20.         {
  21.              try
  22.             {
  23.                  this .KeyPreview =  true  ;
  24.                  this .radioButton1.Checked = true   ;
  25.                 TabHandleEnter( this .Controls);
  26.             }
  27.              catch (Exception ex)
  28.             {
  29.                 Console.Write(ex.GetType().Name +  "的异常产生" );
  30.             }
  31.         }
  32.         #endregion
  33.         #region"功能函数"
  34.          /// <summary>
  35.          /// 按下Enter键后,按照Tab顺,焦点向下一个控件移动
  36.          /// </summary>
  37.          private   void  TabHandleEnter(Control.ControlCollection controls)
  38.         {
  39.              foreach  (Control ctl  in  controls)
  40.             {
  41.                  if  (ctl.GetType().Name.Equals( "GroupBox" ) ||
  42.                     ctl.GetType().Name.Equals( "Panel" ))
  43.                 {
  44.                     TabHandleEnter(ctl.Controls);
  45.                 }
  46.                  else  {
  47.                     ctl.KeyDown +=  new  KeyEventHandler(ctl_EnterKeyDown);
  48.                 }
  49.             }
  50.         }
  51.          /// <summary>
  52.          /// 按下Enter键后,焦点向下一个控件移动
  53.          /// </summary>
  54.          /// <param name="sender"></param>
  55.          /// <param name="e"></param>
  56.          private   void  ctl_EnterKeyDown(Object sender,KeyEventArgs e) 
  57.         {
  58.              if  (e.KeyCode == Keys.Enter)
  59.             {
  60.                 MoveToNextCtrl((Control)sender,  true , cboBoxFirst, txtLast);
  61.             }
  62.         }
  63.          /// <summary>
  64.          /// 移向下一个控件
  65.          /// </summary>
  66.          /// <param name="ctrl">当前的控件</param>
  67.          /// <param name="forward">true:焦点向下一个控件移动; false:焦点不向下一个控件移动</param>
  68.          /// <param name="firstCtrl">Form中第一个控件</param>
  69.          /// <param name="lastCtrl">Form中最后一个控件</param>
  70.          private   void  MoveToNextCtrl(Control ctrl,Boolean forward,Control firstCtrl,Control lastCtrl)
  71.         {
  72.              //按照子控件的 Tab 键顺序向后检索下一个控件
  73.             Control vCtrl = GetNextControl(ctrl, forward);
  74.              if  (ctrl.Equals(firstCtrl) && !forward)
  75.             {
  76.                 MoveToNextCtrl(lastCtrl,forward,firstCtrl,lastCtrl );
  77.                  return ;
  78.             }
  79.              if  (vCtrl ==  null )
  80.             {
  81.                  return ;
  82.             }
  83.              //跳过控件名为"SKIP"或非可激活的或不可见的或不能聚焦的控件
  84.              if  (! "SKIP" .Equals(vCtrl.AccessibleName) &&
  85.                 vCtrl.Enabled && vCtrl.Visible && vCtrl.TabStop)
  86.             {
  87.                  switch (vCtrl.GetType().Name)
  88.                 {
  89.                      case   "GroupBox" :
  90.                         MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
  91.                          break ;
  92.                      case   "Panel" :
  93.                         MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
  94.                          break ;
  95.                      case   "RadioButton" :
  96.                          if  ((vCtrl  as  RadioButton).Checked)
  97.                         {
  98.                             vCtrl.Focus();
  99.                         }
  100.                          else
  101.                         {
  102.                             MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
  103.                         }
  104.                          break ;
  105.                      default :
  106.                         vCtrl.Focus();
  107.                          break ;
  108.                 }
  109.             }
  110.              else  
  111.             {
  112.                 MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
  113.             }
  114.         }
  115.         #endregion
  116.        
  117.     }
  118. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值