开发环境:VS.NET 2005, .net framework 2.0;
开发语言: C# 2.0
窗体设计器生成的代码(Form1.Designer.cs)
- namespace MyTest
- {
- partial class Form1
- {
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null ;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose( bool disposing)
- {
- if (disposing && (components != null ))
- {
- components.Dispose();
- }
- base .Dispose(disposing);
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this .groupBox1 = new System.Windows.Forms.GroupBox();
- this .cboBoxFirst = new System.Windows.Forms.ComboBox();
- this .button1 = new System.Windows.Forms.Button();
- this .txtBox = new System.Windows.Forms.TextBox();
- this .groupBox2 = new System.Windows.Forms.GroupBox();
- this .button2 = new System.Windows.Forms.Button();
- this .txtLast = new System.Windows.Forms.TextBox();
- this .groupBox3 = new System.Windows.Forms.GroupBox();
- this .radioButton2 = new System.Windows.Forms.RadioButton();
- this .radioButton1 = new System.Windows.Forms.RadioButton();
- this .groupBox1.SuspendLayout();
- this .groupBox2.SuspendLayout();
- this .groupBox3.SuspendLayout();
- this .SuspendLayout();
- //
- // groupBox1
- //
- this .groupBox1.Controls.Add( this .cboBoxFirst);
- this .groupBox1.Controls.Add( this .button1);
- this .groupBox1.Controls.Add( this .txtBox);
- this .groupBox1.Location = new System.Drawing.Point(23, 12);
- this .groupBox1.Name = "groupBox1" ;
- this .groupBox1.Size = new System.Drawing.Size(256, 159);
- this .groupBox1.TabIndex = 1;
- this .groupBox1.TabStop = false ;
- this .groupBox1.Text = "groupBox1" ;
- //
- // cboBoxFirst
- //
- this .cboBoxFirst.FormattingEnabled = true ;
- this .cboBoxFirst.Items.AddRange( new object [] {
- "小明" ,
- "校长" ,
- "主任" });
- this .cboBoxFirst.Location = new System.Drawing.Point(7, 33);
- this .cboBoxFirst.Name = "cboBoxFirst" ;
- this .cboBoxFirst.Size = new System.Drawing.Size(121, 20);
- this .cboBoxFirst.TabIndex = 2;
- //
- // button1
- //
- this .button1.AccessibleName = "SKIP" ;
- this .button1.Location = new System.Drawing.Point(122, 85);
- this .button1.Name = "button1" ;
- this .button1.Size = new System.Drawing.Size(52, 23);
- this .button1.TabIndex = 8;
- this .button1.Text = "button1" ;
- this .button1.UseVisualStyleBackColor = true ;
- //
- // txtBox
- //
- this .txtBox.Location = new System.Drawing.Point(6, 87);
- this .txtBox.Name = "txtBox" ;
- this .txtBox.Size = new System.Drawing.Size(100, 19);
- this .txtBox.TabIndex = 4;
- //
- // groupBox2
- //
- this .groupBox2.Controls.Add( this .button2);
- this .groupBox2.Controls.Add( this .txtLast);
- this .groupBox2.Location = new System.Drawing.Point(23, 259);
- this .groupBox2.Name = "groupBox2" ;
- this .groupBox2.Size = new System.Drawing.Size(256, 100);
- this .groupBox2.TabIndex = 28;
- this .groupBox2.TabStop = false ;
- this .groupBox2.Text = "groupBox2" ;
- //
- // button2
- //
- this .button2.AccessibleName = "SKIP" ;
- this .button2.Location = new System.Drawing.Point(122, 42);
- this .button2.Name = "button2" ;
- this .button2.Size = new System.Drawing.Size(60, 23);
- this .button2.TabIndex = 36;
- this .button2.Text = "button2" ;
- this .button2.UseVisualStyleBackColor = true ;
- //
- // txtLast
- //
- this .txtLast.Location = new System.Drawing.Point(7, 46);
- this .txtLast.Name = "txtLast" ;
- this .txtLast.Size = new System.Drawing.Size(100, 19);
- this .txtLast.TabIndex = 32;
- //
- // groupBox3
- //
- this .groupBox3.Controls.Add( this .radioButton2);
- this .groupBox3.Controls.Add( this .radioButton1);
- this .groupBox3.Location = new System.Drawing.Point(23, 177);
- this .groupBox3.Name = "groupBox3" ;
- this .groupBox3.Size = new System.Drawing.Size(256, 65);
- this .groupBox3.TabIndex = 24;
- this .groupBox3.TabStop = false ;
- this .groupBox3.Text = "groupBox3" ;
- //
- // radioButton2
- //
- this .radioButton2.AutoSize = true ;
- this .radioButton2.Location = new System.Drawing.Point(110, 29);
- this .radioButton2.Name = "radioButton2" ;
- this .radioButton2.Size = new System.Drawing.Size(88, 16);
- this .radioButton2.TabIndex = 32;
- this .radioButton2.TabStop = true ;
- this .radioButton2.Text = "radioButton2" ;
- this .radioButton2.UseVisualStyleBackColor = true ;
- //
- // radioButton1
- //
- this .radioButton1.AutoSize = true ;
- this .radioButton1.Location = new System.Drawing.Point(6, 29);
- this .radioButton1.Name = "radioButton1" ;
- this .radioButton1.Size = new System.Drawing.Size(88, 16);
- this .radioButton1.TabIndex = 28;
- this .radioButton1.TabStop = true ;
- this .radioButton1.Text = "radioButton1" ;
- this .radioButton1.UseVisualStyleBackColor = true ;
- //
- // Form1
- //
- this .AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this .ClientSize = new System.Drawing.Size(524, 380);
- this .Controls.Add( this .groupBox3);
- this .Controls.Add( this .groupBox2);
- this .Controls.Add( this .groupBox1);
- this .Name = "Form1" ;
- this .Text = "Form1" ;
- this .Load += new System.EventHandler( this .Form1_Load);
- this .groupBox1.ResumeLayout( false );
- this .groupBox1.PerformLayout();
- this .groupBox2.ResumeLayout( false );
- this .groupBox2.PerformLayout();
- this .groupBox3.ResumeLayout( false );
- this .groupBox3.PerformLayout();
- this .ResumeLayout( false );
- }
- #endregion
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.TextBox txtBox;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.TextBox txtLast;
- private System.Windows.Forms.ComboBox cboBoxFirst;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.RadioButton radioButton2;
- private System.Windows.Forms.RadioButton radioButton1;
- }
- }
Form关联文件(功能实现代码):
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace MyTest
- {
- public partial class Form1 : Form
- {
- #region"页面控件初始化"
- public Form1()
- {
- InitializeComponent();
- }
- #endregion
- #region"页面初始化(Form_Load)"
- private void Form1_Load( object sender, EventArgs e)
- {
- try
- {
- this .KeyPreview = true ;
- this .radioButton1.Checked = true ;
- TabHandleEnter( this .Controls);
- }
- catch (Exception ex)
- {
- Console.Write(ex.GetType().Name + "的异常产生" );
- }
- }
- #endregion
- #region"功能函数"
- /// <summary>
- /// 按下Enter键后,按照Tab顺,焦点向下一个控件移动
- /// </summary>
- private void TabHandleEnter(Control.ControlCollection controls)
- {
- foreach (Control ctl in controls)
- {
- if (ctl.GetType().Name.Equals( "GroupBox" ) ||
- ctl.GetType().Name.Equals( "Panel" ))
- {
- TabHandleEnter(ctl.Controls);
- }
- else {
- ctl.KeyDown += new KeyEventHandler(ctl_EnterKeyDown);
- }
- }
- }
- /// <summary>
- /// 按下Enter键后,焦点向下一个控件移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ctl_EnterKeyDown(Object sender,KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- MoveToNextCtrl((Control)sender, true , cboBoxFirst, txtLast);
- }
- }
- /// <summary>
- /// 移向下一个控件
- /// </summary>
- /// <param name="ctrl">当前的控件</param>
- /// <param name="forward">true:焦点向下一个控件移动; false:焦点不向下一个控件移动</param>
- /// <param name="firstCtrl">Form中第一个控件</param>
- /// <param name="lastCtrl">Form中最后一个控件</param>
- private void MoveToNextCtrl(Control ctrl,Boolean forward,Control firstCtrl,Control lastCtrl)
- {
- //按照子控件的 Tab 键顺序向后检索下一个控件
- Control vCtrl = GetNextControl(ctrl, forward);
- if (ctrl.Equals(firstCtrl) && !forward)
- {
- MoveToNextCtrl(lastCtrl,forward,firstCtrl,lastCtrl );
- return ;
- }
- if (vCtrl == null )
- {
- return ;
- }
- //跳过控件名为"SKIP"或非可激活的或不可见的或不能聚焦的控件
- if (! "SKIP" .Equals(vCtrl.AccessibleName) &&
- vCtrl.Enabled && vCtrl.Visible && vCtrl.TabStop)
- {
- switch (vCtrl.GetType().Name)
- {
- case "GroupBox" :
- MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
- break ;
- case "Panel" :
- MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
- break ;
- case "RadioButton" :
- if ((vCtrl as RadioButton).Checked)
- {
- vCtrl.Focus();
- }
- else
- {
- MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
- }
- break ;
- default :
- vCtrl.Focus();
- break ;
- }
- }
- else
- {
- MoveToNextCtrl(vCtrl, forward, firstCtrl, lastCtrl);
- }
- }
- #endregion
- }
- }