教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> .NET类 >> ASP.NET >> 自己写的label 与 textBox 组合的控件 RSS订阅
自己写的label 与 textBox 组合的控件
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
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();
}
}
}





来源:upschool.com.cn
作者:
关键字:自己写,label,textBox,组合,控件
发表日期:2005-12-23

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:正则表达式   下一篇:系统出错信息设计


本文的相类似文章
  • 使用嵌套的Repeater控件显示分级数据
  • 计算多行TextBox的行数
  • 自制IE风格按纽控件
  • 利用Internet传输控件来使用FTP
  • 模拟TreeView控件的HoverSelection属性
  • 在VB6中将XML传入一个TreeView控件
  • 轻松调整VB.NET控件
  • 从数组中加载组合框和列表框
  • 从图片框控件取得颜色
  • VB制作一个通信卡片ActiveX控件
  • 网友评论 查看本文全部评论
    笔 名: *
    评 论:
    最多500字。当前字数:0
    联系方式:
    验证码:
    在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com