分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有