可用来显示空值的时间选择控件4

类别:.NET开发 点击:0 评论:0 推荐:

#region 组件设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器

         /// 修改此方法的内容。

         /// </summary>

          private void InitializeComponent()

         {

              this.panel3 = new System.Windows.Forms.Panel();

              this.panel1 = new System.Windows.Forms.Panel();

              this.panel2 = new System.Windows.Forms.Panel();

              this.lbSpace = new System.Windows.Forms.Label();

              this.lbSep3 = new System.Windows.Forms.Label();

              this.tbDay = new System.Windows.Forms.TextBox();

              this.lbSep2 = new System.Windows.Forms.Label();

              this.tbMonth = new System.Windows.Forms.TextBox();

              this.lbSep1 = new System.Windows.Forms.Label();

              this.tbYear = new System.Windows.Forms.TextBox();

              this.label2 = new System.Windows.Forms.Label();

              this.btnMain = new System.Windows.Forms.Button();

              this.UpDown = new System.Windows.Forms.VScrollBar();

              this.panel3.SuspendLayout();

              this.panel1.SuspendLayout();

              this.panel2.SuspendLayout();

              this.SuspendLayout();

              //

              // panel3

              //

              this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

              this.panel3.Controls.Add(this.panel1);

              this.panel3.Controls.Add(this.btnMain);

              this.panel3.Controls.Add(this.UpDown);

              this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;

              this.panel3.Location = new System.Drawing.Point(0, 0);

              this.panel3.Name = "panel3";

              this.panel3.Size = new System.Drawing.Size(104, 24);

              this.panel3.TabIndex = 210;

              //

              // panel1

              //

              this.panel1.Controls.Add(this.panel2);

              this.panel1.Controls.Add(this.label2);

              this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;

              this.panel1.Location = new System.Drawing.Point(0, 0);

              this.panel1.Name = "panel1";

              this.panel1.Size = new System.Drawing.Size(84, 20);

              this.panel1.TabIndex = 212;

              //

              // panel2

              //

              this.panel2.Controls.Add(this.lbSpace);

              this.panel2.Controls.Add(this.lbSep3);

              this.panel2.Controls.Add(this.tbDay);

              this.panel2.Controls.Add(this.lbSep2);

              this.panel2.Controls.Add(this.tbMonth);

              this.panel2.Controls.Add(this.lbSep1);

              this.panel2.Controls.Add(this.tbYear);

              this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;

              this.panel2.Location = new System.Drawing.Point(0, 2);

              this.panel2.Name = "panel2";

              this.panel2.Size = new System.Drawing.Size(84, 18);

              this.panel2.TabIndex = 207;

              //

              // lbSpace

              //

              this.lbSpace.BackColor = System.Drawing.SystemColors.Window;

              this.lbSpace.Dock = System.Windows.Forms.DockStyle.Fill;

              this.lbSpace.Location = new System.Drawing.Point(70, 0);

              this.lbSpace.Name = "lbSpace";

              this.lbSpace.Size = new System.Drawing.Size(14, 18);

              this.lbSpace.TabIndex = 223;

              this.lbSpace.TextAlign = System.Drawing.ContentAlignment.TopCenter;

              this.lbSpace.Click += new System.EventHandler(this.lbSpace_Click);

              this.lbSpace.EnabledChanged += new System.EventHandler(this.lbSep1_EnabledChanged);

              //

              // lbSep3

              //

              this.lbSep3.AutoSize = true;

              this.lbSep3.BackColor = System.Drawing.SystemColors.Window;

              this.lbSep3.Dock = System.Windows.Forms.DockStyle.Left;

              this.lbSep3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

              this.lbSep3.Location = new System.Drawing.Point(70, 0);

              this.lbSep3.Name = "lbSep3";

              this.lbSep3.Size = new System.Drawing.Size(0, 17);

              this.lbSep3.TabIndex = 220;

              this.lbSep3.EnabledChanged += new System.EventHandler(this.lbSep1_EnabledChanged);

              //

              // tbDay

              //

              this.tbDay.AutoSize = false;

              this.tbDay.BorderStyle = System.Windows.Forms.BorderStyle.None;

              this.tbDay.Dock = System.Windows.Forms.DockStyle.Left;

              this.tbDay.Location = new System.Drawing.Point(58, 0);

              this.tbDay.Name = "tbDay";

              this.tbDay.Size = new System.Drawing.Size(12, 18);

              this.tbDay.TabIndex = 219;

              this.tbDay.Text = "12";

              this.tbDay.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

              this.tbDay.WordWrap = false;

              this.tbDay.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbMonth_KeyDown);

              this.tbDay.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tbDay_KeyPress);

              //

              // lbSep2

              //

              this.lbSep2.AutoSize = true;

              this.lbSep2.BackColor = System.Drawing.SystemColors.Window;

              this.lbSep2.Dock = System.Windows.Forms.DockStyle.Left;

              this.lbSep2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

              this.lbSep2.Location = new System.Drawing.Point(47, 0);

              this.lbSep2.Name = "lbSep2";

              this.lbSep2.Size = new System.Drawing.Size(11, 17);

              this.lbSep2.TabIndex = 218;

              this.lbSep2.Text = "-";

              this.lbSep2.EnabledChanged += new System.EventHandler(this.lbSep1_EnabledChanged);

              //

              // tbMonth

              //

              this.tbMonth.AutoSize = false;

              this.tbMonth.BorderStyle = System.Windows.Forms.BorderStyle.None;

              this.tbMonth.Dock = System.Windows.Forms.DockStyle.Left;

              this.tbMonth.Location = new System.Drawing.Point(35, 0);

              this.tbMonth.Name = "tbMonth";

              this.tbMonth.Size = new System.Drawing.Size(12, 18);

              this.tbMonth.TabIndex = 217;

              this.tbMonth.Text = "12";

              this.tbMonth.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

              this.tbMonth.WordWrap = false;

              this.tbMonth.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbMonth_KeyDown);

              this.tbMonth.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tbMonth_KeyPress);

              this.tbMonth.Leave += new System.EventHandler(this.tbMonth_Leave);

              this.tbMonth.Enter += new System.EventHandler(this.tbMonth_Enter);

              //

              // lbSep1

              //

              this.lbSep1.AutoSize = true;

              this.lbSep1.BackColor = System.Drawing.SystemColors.Window;

              this.lbSep1.Dock = System.Windows.Forms.DockStyle.Left;

              this.lbSep1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

              this.lbSep1.Location = new System.Drawing.Point(24, 0);

              this.lbSep1.Name = "lbSep1";

              this.lbSep1.Size = new System.Drawing.Size(11, 17);

              this.lbSep1.TabIndex = 216;

              this.lbSep1.Text = "-";

              this.lbSep1.EnabledChanged += new System.EventHandler(this.lbSep1_EnabledChanged);

              //

              // tbYear

              //

              this.tbYear.AutoSize = false;

              this.tbYear.BorderStyle = System.Windows.Forms.BorderStyle.None;

              this.tbYear.Dock = System.Windows.Forms.DockStyle.Left;

              this.tbYear.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

              this.tbYear.Location = new System.Drawing.Point(0, 0);

              this.tbYear.Name = "tbYear";

              this.tbYear.RightToLeft = System.Windows.Forms.RightToLeft.No;

              this.tbYear.Size = new System.Drawing.Size(24, 18);

              this.tbYear.TabIndex = 211;

              this.tbYear.Text = "2004";

              this.tbYear.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

              this.tbYear.WordWrap = false;

              this.tbYear.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbMonth_KeyDown);

              this.tbYear.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tbYear_KeyPress);

              this.tbYear.Leave += new System.EventHandler(this.tbYear_Leave);

              this.tbYear.Enter += new System.EventHandler(this.tbMonth_Enter);

              //

              // label2

              //

              this.label2.BackColor = System.Drawing.SystemColors.Window;

              this.label2.Dock = System.Windows.Forms.DockStyle.Top;

              this.label2.Location = new System.Drawing.Point(0, 0);

              this.label2.Name = "label2";

              this.label2.Size = new System.Drawing.Size(84, 2);

              this.label2.TabIndex = 206;

              this.label2.EnabledChanged += new System.EventHandler(this.lbSep1_EnabledChanged);

              //

              // btnMain

              //

              this.btnMain.Dock = System.Windows.Forms.DockStyle.Right;

              this.btnMain.Location = new System.Drawing.Point(84, 0);

              this.btnMain.Name = "btnMain";

              this.btnMain.Size = new System.Drawing.Size(16, 20);

              this.btnMain.TabIndex = 811;

              this.btnMain.Click += new System.EventHandler(this.btnMain_Click);

              this.btnMain.Paint += new System.Windows.Forms.PaintEventHandler(this.btnMain_Paint);

              //

              // UpDown

              //

              this.UpDown.Dock = System.Windows.Forms.DockStyle.Right;

              this.UpDown.Location = new System.Drawing.Point(100, 0);

              this.UpDown.Name = "UpDown";

              this.UpDown.Size = new System.Drawing.Size(0, 20);

              this.UpDown.TabIndex = 210;

              this.UpDown.Visible = false;

              this.UpDown.Scroll += new System.Windows.Forms.ScrollEventHandler(this.UpDown_Scroll);

              //

              // DateInput

              //

              this.Controls.Add(this.panel3);

              this.Name = "DateInput";

              this.Size = new System.Drawing.Size(104, 24);

              this.Leave += new System.EventHandler(this.DateInput_Leave);

              this.panel3.ResumeLayout(false);

              this.panel1.ResumeLayout(false);

              this.panel2.ResumeLayout(false);

              this.ResumeLayout(false);

 

         }

          #endregion

 

          private void tbYear_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

         {

              if (this.DesignMode)

                   return ;

              TextBox obj = (TextBox)sender ;

              obj.SelectedText = "" ;

              string strValue = obj.Text ;

              int iLen= strValue.ToString().Length ;

              int key = (int)(e.KeyChar) ;

              switch(key)

              {

                   case 48://0                      

                   case 49://1

                   case 50://2

                   case 51://3

                   case 52://4

                   case 53://5

                   case 54://6

                   case 55://7

                   case 56://8

                   case 57://9

                        e.Handled = false ; 

                       if (iLen == 3)

                            obj.Parent.SelectNextControl(obj,true,true,false,true) ;

                       else if (iLen == 4)

                            e.Handled = true ;  

                       break ;

                   case 45://-                      

                            e.Handled = true ;  

                       break ;

                   case 8://backspace

                   case 27://esc

                        e.Handled = false ; 

                       break ;

                   default :

                        e.Handled = true ;

                       break ;

              }    

         }

 

          private void tbMonth_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

         {

              if (this.DesignMode)

                   return ;

              TextBox obj = (TextBox)sender ;

              obj.SelectedText = "" ;

              string strValue = obj.Text ;

              int iLen= strValue.ToString().Length ;

              int key = (int)(e.KeyChar) ;

              switch(key)

              {

                   case 48://0                      

                   case 49://1

                   case 50://2

                   case 51://3

                   case 52://4

                   case 53://5

                   case 54://6

                   case 55://7

                   case 56://8

                   case 57://9        

                       if (iLen == 0)

                            e.Handled = false ; 

                       else if (iLen == 1)//已经输入了一位

                       {

                            e.Handled = true ;   //默认不处理,后面分析需要处理的情况

                            if (key==48)//现在输入的是0

                            {

                                 if(obj.SelectionStart == 0)//插入

                                 {

                                     if (strValue != "0")//已经输入的不是0

                                          e.Handled = false ;

                                 }

                                 else if (strValue == "1")//追加且已经输入了1

                                      e.Handled = false ;                                                                       

                            }

                            else//输入的是非0数字

                            {

                                 if (strValue == "0")//已经输入了一个0

                                 {

                                     if (obj.SelectionStart == 0)//前面插入数字

                                     {

                                          if (key==49)//只能插入1

                                               e.Handled = false ; 

                                     }

                                     else

                                          e.Handled = false ; 

 

                                 }

                                 else if (strValue == "1")//已经输入了一个1

                                 {

                                     if (obj.SelectionStart == 0)//前面插入数字

                                     {

                                          if (key==49)//只能插入0,1(前面已处理了0的情况)

                                               e.Handled = false ; 

                                     }

                                     else if (key==49||key==50)//0,1,2

                                          e.Handled = false ; 

                                 }

                                 else if (strValue == "2")//已经输入了一个2

                                 {

                                     if (obj.SelectionStart == 0&&(key==49))//前面插入数字只能插入0,1

                                          e.Handled = false ; 

                                 }

                            }

                            if (e.Handled == false)

                            obj.Parent.SelectNextControl(obj,true,true,false,true) ;

                       }

                       else

                            e.Handled = true ;  

                       break ;

                   case 45://-                      

                        e.Handled = true ;  

                       break ;

                   case 8://backspace

                       if (iLen == 0)

                            obj.Parent.SelectNextControl(obj,false,true,false,true) ;

                       break ;

                   case 27://esc

                        e.Handled = false ; 

                       break ;

                   default :

                        e.Handled = true ;

                       break ;

              }    

         }

本文地址:http://com.8s8s.com/it/it42028.htm