自己写的label 与 textBox 组合的控件

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

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions ;

namespace MyWinControl
{
 /// <summary>
 /// UserControl1 的摘要说明。
 /// </summary>
 public class lblTextBox : System.Windows.Forms.UserControl
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Splitter splitter1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.ToolTip toolTip1;
  private System.ComponentModel.IContainer components;

  public lblTextBox()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();

   // TODO: 在 InitComponent 调用后添加任何初始化

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.label1 = new System.Windows.Forms.Label();
   this.splitter1 = new System.Windows.Forms.Splitter();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Dock = System.Windows.Forms.DockStyle.Left;
   this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.label1.Location = new System.Drawing.Point(0, 0);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(60, 24);
   this.label1.TabIndex = 1;
   this.label1.Text = "labelText";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // splitter1
   //
   this.splitter1.Location = new System.Drawing.Point(60, 0);
   this.splitter1.Name = "splitter1";
   this.splitter1.Size = new System.Drawing.Size(24, 24);
   this.splitter1.TabIndex = 2;
   this.splitter1.TabStop = false;
   this.splitter1.Visible = false;
   //
   // textBox1
   //
   this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.textBox1.Location = new System.Drawing.Point(84, 0);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(116, 21);
   this.textBox1.TabIndex = 3;
   this.textBox1.Text = "textBox1";
   this.textBox1.LostFocus += new System.EventHandler(this.textBox1_LostFocus);
   this.textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);
   this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
   this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
   //
   // lblTextBox
   //
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.splitter1);
   this.Controls.Add(this.label1);
   this.Name = "lblTextBox";
   this.Size = new System.Drawing.Size(200, 24);
   this.GotFocus += new System.EventHandler(this.lblTextBox_GotFocus);
   this.ResumeLayout(false);

  }
  #endregion

  #region//属性
  [
  Category("Appearance"),
  Description("标签的宽度"),
  DefaultValue(100),
  Bindable(true),
  ]
  public int lblWidth
  {
   get
   {
    return label1.Width ;
   }
   set
   {
    label1.Width=value;

    //    //Raise property changed event for DrawingMode
    //    OnDrawingModeChanged(EventArgs.Empty);
   }
  }
  [
  Category("Appearance"),
  Description("标签的内容"),
  DefaultValue("标签"),
  Bindable(true),
  ]
  public string lblCaption
  {
   get
   {
    return label1.Text  ;
   }
   set
   {
    label1.Text=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本框的内容"),
  DefaultValue("text"),
  Bindable(true),
  ]
  public string textBoxText
  {
   get
   {
    return textBox1.Text ;
   }
   set
   {
    textBox1.Text=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本框是否为密码"),
  //DefaultValue(false),
  Bindable(true),
  ]
  public char textBoxTextpass
  {
   //   get
   //   {
   //    //return textBox1.PasswordChar ;
   //   }
   set
   {
    textBox1.PasswordChar=value;

   }
  }
  [
  Category("Appearance"),
  Description("判断输入的字符"),
  DefaultValue(TextType.All),
  Bindable(true),
  ]
  public TextType textType
  {
   get
   {
    return TtextType ;
    
   }
   set
   {
    TtextType=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本框最大长度"),
  DefaultValue(0),
  Bindable(true),
  ]
  public int MaxLength
  {
   get
   {
    return textBox1.MaxLength  ;
    
   }
   set
   {
    textBox1.MaxLength=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本数据最大值"),
  DefaultValue(0),
  Bindable(true),
  ]
  public int max
  {
   get
   {
    return tmax  ;
    
   }
   set
   {
    tmax=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本数据最小值"),
  DefaultValue(0),
  Bindable(true),
  ]
  public int min
  {
   get
   {
    return tmin  ;
    
   }
   set
   {
    tmin=value;

   }
  }
  [
  Category("Appearance"),
  Description("文本可编辑性"),
  //DefaultValue(0),
  Bindable(true),
  ]
  public bool textEnabled
  {
   get
   {
    return textBox1.Enabled   ;
    
   }
   set
   {
    textBox1.Enabled=value;

   }
  }
  [
  Category("Appearance"),
  Description("帮助文件"),
  DefaultValue("※help※"),
  Bindable(true),
  ]
  public string textHelp
  {
   get
   {
    return ttextHelp   ;
    
   }
   set
   {
    ttextHelp=value;

   }
  }
  [
  Category("Appearance"),
  Description("按回车是否换焦点"),
  DefaultValue(true),
  Bindable(true),
  ]
  public bool EnterToTap
  {
   get
   {
    return tEnterToTap;
    
   }
   set
   {
    tEnterToTap=value;

   }
  }
  #endregion
  #region//方法
  private bool isNumeric(char ch)
  {
   if(char.IsNumber(ch))
   {
    return true;
   }
   return false;
  }
  private bool isLetter(char ch)
  {
   if (char.IsLetter(ch))
   {
   return true;
   }
   return false;
  }
  private bool isNumericorLetter(char ch)
  {
   if (char.IsLetterOrDigit(ch))
   {
    return true;
   }
   return false;
  }

  #endregion
  #region//自带事件
  internal void textBox1_TextChanged(object sender, System.EventArgs e)
  {
//   Regex DoubleQuotedString = new Regex("/[^\d]/g,''",System.Text.RegularExpressions.RegexOptions.None );

    OnlblTextChanged(this,new EventArgs());

   //textBox1.Text.Replace("",(char)Keys.Cancel);

  }
  internal void textBox1_LostFocus(object sender, EventArgs e)
  {
   char[] tmps;
   tmps =textBox1.Text.ToCharArray();
   switch (TtextType)
   {
    case TextType.Letter:
    case TextType.Numberic:
    case TextType.NumericorLetter:
    
     foreach(char tmp in tmps)
     {
      if (char.IsDigit(tmp)|| char.IsLetter(tmp))
      {
     
       //tmp =(char)(Keys.Cancel);
      }
      else
      {
       textBox1.Text="";
      }
     }
    
    break;
    case TextType.decimalfraction :
    break;
     default:
    break;
    
   }
   }
  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
   /*/////////////////////////////////////////
   ///****************************************
   ///判断输入的是不是数字、字母   //
   ///TextType.Numberic: 数字    // 
   ///TextType.NumericorLetter :字母  //  
   ///TextType.NumericorLetter:   //
   ********************************************
   *//////////////////////////////////////////
   switch(e.KeyChar)
   {
    case (char)( Keys.Back) :
     break;
    case (char)(Keys.Enter) :
     if(tEnterToTap)
     SendKeys.Send("{TAB}");
     break;
     default:
     switch(TtextType)
     {
       case TextType.Numberic :
        if (!isNumeric(e.KeyChar)|e.KeyChar.Equals(Keys.Back))
        { //Int16.MaxValue
         e.Handled =true;
         break;
        }
       
        if (tmax != 0)
        {
          if( textBox1.Text.Length >0)
           {
           if(int.Parse(textBox1.Text+e.KeyChar.ToString())>tmax)
           {e.Handled =true;
           break;
           }
            
            
           }
         
         
        }
        if (tmin != 0)
        {
         if( textBox1.Text.Length >0)
         {
          if(int.Parse(textBox1.Text)<tmin)
          {
           e.Handled =true;
           break;
          }
         }
        }
         break;
      case TextType.Letter :
       if (!isLetter(e.KeyChar))
        e.Handled =true;
        //SendKeys.Send("/");
       break;
      case TextType.NumericorLetter :
       if (!isNumericorLetter(e.KeyChar))
        e.Handled =true;
       break;
      case TextType.decimalfraction :
       if(!char.IsDigit(e.KeyChar))
        if(e.KeyChar=='.' )
        {
         if(textBox1.Text.IndexOf('.')!=-1)
          e.Handled =true;
        }
        else
        {
         e.Handled =true;
        }

       break;
      case TextType.All :
      default:
       break;
     }
     break;

   }
   
   
  }
  #endregion
  #region //自定义事件
  public event MyEvent lblTextChanged;
  public delegate void MyEvent(object sender,System.EventArgs e);
  protected virtual void OnlblTextChanged(object sender ,System.EventArgs e)
  {
   if (lblTextChanged!=null)
    lblTextChanged(this,e);
  }

  #endregion
  #region//枚举
 public enum TextType
  {
  All,
  Numberic,
  Letter,
  NumericorLetter,
  decimalfraction
  }
  #endregion
  public TextType TtextType;
  internal int tmax;//文本框最大值
  internal int tmin;//最小值
  internal string ttextHelp;//帮助内容
  internal bool tEnterToTap =true;//按回车转到换行

  private void textBox1_GotFocus(object sender, EventArgs e)
  {
   this.toolTip1.SetToolTip(this.textBox1,ttextHelp);
   if( TtextType !=TextType.All )
   textBox1.ImeMode =System.Windows.Forms.ImeMode.Disable  ;
  }
  private void lblTextBox_GotFocus(object sender, EventArgs e)
  {
   textBox1.Select();
  }
 }
}

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