带有小计算器的TextBox控件
/*hycapril@sohu.com
需要的添加的控件,
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button B_1;
private System.Windows.Forms.Button B_N1;
private System.Windows.Forms.Button B_N2;
private System.Windows.Forms.Button B_N3;
private System.Windows.Forms.Button B_N4;
private System.Windows.Forms.Button B_N5;
private System.Windows.Forms.Button B_N6;
private System.Windows.Forms.Button B_N7;
private System.Windows.Forms.Button B_N8;
private System.Windows.Forms.Button B_N9;
private System.Windows.Forms.Button B_N0;
private System.Windows.Forms.Button B_ok;
private System.Windows.Forms.Button B_Cancel;
private System.Windows.Forms.Button B_J;
private System.Windows.Forms.Button B_JJ;
private System.Windows.Forms.Button B_C;
private System.Windows.Forms.Button B_CC;
private System.Windows.Forms.Button B_D;
private System.Windows.Forms.Button B_B;
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace TextBoxControl
{
/// <summary>
/// TextBoxControl 的摘要说明。
/// 带有小计算器的TextBox控件。
/// hycapril 2005 04 06制作。
/// </summary>
public class TextBoxControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button B_1;
private System.Windows.Forms.Button B_N1;
private System.Windows.Forms.Button B_N2;
private System.Windows.Forms.Button B_N3;
private System.Windows.Forms.Button B_N4;
private System.Windows.Forms.Button B_N5;
private System.Windows.Forms.Button B_N6;
private System.Windows.Forms.Button B_N7;
private System.Windows.Forms.Button B_N8;
private System.Windows.Forms.Button B_N9;
private System.Windows.Forms.Button B_N0;
private System.Windows.Forms.Button B_ok;
private System.Windows.Forms.Button B_Cancel;
private System.Windows.Forms.Button B_J;
private System.Windows.Forms.Button B_JJ;
private System.Windows.Forms.Button B_C;
private System.Windows.Forms.Button B_CC;
private System.Windows.Forms.Button B_D;
private System.Windows.Forms.Button B_B;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public TextBoxControl()
{
// 该调用是 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TextBoxControl));
this.textBox1 = new System.Windows.Forms.TextBox();
this.B_1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.B_B = new System.Windows.Forms.Button();
this.B_D = new System.Windows.Forms.Button();
this.B_CC = new System.Windows.Forms.Button();
this.B_C = new System.Windows.Forms.Button();
this.B_JJ = new System.Windows.Forms.Button();
this.B_J = new System.Windows.Forms.Button();
this.B_Cancel = new System.Windows.Forms.Button();
this.B_N0 = new System.Windows.Forms.Button();
this.B_N9 = new System.Windows.Forms.Button();
this.B_N8 = new System.Windows.Forms.Button();
this.B_N7 = new System.Windows.Forms.Button();
this.B_N6 = new System.Windows.Forms.Button();
this.B_N5 = new System.Windows.Forms.Button();
this.B_N4 = new System.Windows.Forms.Button();
this.B_N3 = new System.Windows.Forms.Button();
this.B_N2 = new System.Windows.Forms.Button();
this.B_N1 = new System.Windows.Forms.Button();
this.B_ok = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// B_1
//
this.B_1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.B_1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.B_1.Image = ((System.Drawing.Image)(resources.GetObject("B_1.Image")));
this.B_1.Location = new System.Drawing.Point(96, 0);
this.B_1.Name = "B_1";
this.B_1.Size = new System.Drawing.Size(18, 20);
this.B_1.TabIndex = 1;
this.B_1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panel1.Controls.Add(this.B_B);
this.panel1.Controls.Add(this.B_D);
this.panel1.Controls.Add(this.B_CC);
this.panel1.Controls.Add(this.B_C);
this.panel1.Controls.Add(this.B_JJ);
this.panel1.Controls.Add(this.B_J);
this.panel1.Controls.Add(this.B_Cancel);
this.panel1.Controls.Add(this.B_N0);
this.panel1.Controls.Add(this.B_N9);
this.panel1.Controls.Add(this.B_N8);
this.panel1.Controls.Add(this.B_N7);
this.panel1.Controls.Add(this.B_N6);
this.panel1.Controls.Add(this.B_N5);
this.panel1.Controls.Add(this.B_N4);
this.panel1.Controls.Add(this.B_N3);
this.panel1.Controls.Add(this.B_N2);
this.panel1.Controls.Add(this.B_N1);
this.panel1.Controls.Add(this.B_ok);
this.panel1.Location = new System.Drawing.Point(8, 24);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(96, 120);
this.panel1.TabIndex = 2;
//
// B_B
//
this.B_B.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_B.Location = new System.Drawing.Point(48, 0);
this.B_B.Name = "B_B";
this.B_B.Size = new System.Drawing.Size(48, 20);
this.B_B.TabIndex = 18;
this.B_B.Text = "<--";
this.B_B.Click += new System.EventHandler(this.B_B_Click);
//
// B_D
//
this.B_D.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_D.Location = new System.Drawing.Point(48, 80);
this.B_D.Name = "B_D";
this.B_D.Size = new System.Drawing.Size(24, 20);
this.B_D.TabIndex = 17;
this.B_D.Text = ".";
this.B_D.Click += new System.EventHandler(this.B_D_Click);
//
// B_CC
//
this.B_CC.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_CC.Location = new System.Drawing.Point(72, 80);
this.B_CC.Name = "B_CC";
this.B_CC.Size = new System.Drawing.Size(24, 20);
this.B_CC.TabIndex = 16;
this.B_CC.Text = "http://www.cuoxin.com/";
this.B_CC.Click += new System.EventHandler(this.B_CC_Click);
//
// B_C
//
this.B_C.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_C.Location = new System.Drawing.Point(72, 60);
this.B_C.Name = "B_C";
this.B_C.Size = new System.Drawing.Size(24, 20);
this.B_C.TabIndex = 15;
this.B_C.Text = "*";
this.B_C.Click += new System.EventHandler(this.B_C_Click);
//
// B_JJ
//
this.B_JJ.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_JJ.Location = new System.Drawing.Point(72, 40);
this.B_JJ.Name = "B_JJ";
this.B_JJ.Size = new System.Drawing.Size(24, 20);
this.B_JJ.TabIndex = 14;
this.B_JJ.Text = "-";
this.B_JJ.Click += new System.EventHandler(this.B_JJ_Click);
//
// B_J
//
this.B_J.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_J.Location = new System.Drawing.Point(72, 20);
this.B_J.Name = "B_J";
this.B_J.Size = new System.Drawing.Size(24, 20);
this.B_J.TabIndex = 13;
this.B_J.Text = "+";
this.B_J.Click += new System.EventHandler(this.B_J_Click);
//
// B_Cancel
//
this.B_Cancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_Cancel.Location = new System.Drawing.Point(0, 0);
this.B_Cancel.Name = "B_Cancel";
this.B_Cancel.Size = new System.Drawing.Size(48, 20);
this.B_Cancel.TabIndex = 11;
this.B_Cancel.Text = "Clear";
this.B_Cancel.Click += new System.EventHandler(this.B_Cancel_Click);
//
// B_N0
//
this.B_N0.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N0.Location = new System.Drawing.Point(0, 80);
this.B_N0.Name = "B_N0";
this.B_N0.Size = new System.Drawing.Size(48, 20);
this.B_N0.TabIndex = 10;
this.B_N0.Text = "0";
this.B_N0.Click += new System.EventHandler(this.B_N0_Click);
//
// B_N9
//
this.B_N9.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N9.Location = new System.Drawing.Point(48, 60);
this.B_N9.Name = "B_N9";
this.B_N9.Size = new System.Drawing.Size(24, 20);
this.B_N9.TabIndex = 9;
this.B_N9.Text = "9";
this.B_N9.Click += new System.EventHandler(this.B_N9_Click);
//
// B_N8
//
this.B_N8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N8.Location = new System.Drawing.Point(24, 60);
this.B_N8.Name = "B_N8";
this.B_N8.Size = new System.Drawing.Size(24, 20);
this.B_N8.TabIndex = 8;
this.B_N8.Text = "8";
this.B_N8.Click += new System.EventHandler(this.B_N8_Click);
//
// B_N7
//
this.B_N7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N7.Location = new System.Drawing.Point(0, 60);
this.B_N7.Name = "B_N7";
this.B_N7.Size = new System.Drawing.Size(24, 20);
this.B_N7.TabIndex = 7;
this.B_N7.Text = "7";
this.B_N7.Click += new System.EventHandler(this.B_N7_Click);
//
// B_N6
//
this.B_N6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N6.Location = new System.Drawing.Point(48, 40);
this.B_N6.Name = "B_N6";
this.B_N6.Size = new System.Drawing.Size(24, 20);
this.B_N6.TabIndex = 6;
this.B_N6.Text = "6";
this.B_N6.Click += new System.EventHandler(this.B_N6_Click);
//
// B_N5
//
this.B_N5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N5.Location = new System.Drawing.Point(24, 40);
this.B_N5.Name = "B_N5";
this.B_N5.Size = new System.Drawing.Size(24, 20);
this.B_N5.TabIndex = 5;
this.B_N5.Text = "5";
this.B_N5.Click += new System.EventHandler(this.B_N5_Click);
//
// B_N4
//
this.B_N4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N4.Location = new System.Drawing.Point(0, 40);
this.B_N4.Name = "B_N4";
this.B_N4.Size = new System.Drawing.Size(24, 20);
this.B_N4.TabIndex = 4;
this.B_N4.Text = "4";
this.B_N4.Click += new System.EventHandler(this.B_N4_Click);
//
// B_N3
//
this.B_N3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N3.Location = new System.Drawing.Point(48, 20);
this.B_N3.Name = "B_N3";
this.B_N3.Size = new System.Drawing.Size(24, 20);
this.B_N3.TabIndex = 3;
this.B_N3.Text = "3";
this.B_N3.Click += new System.EventHandler(this.B_N3_Click);
//
// B_N2
//
this.B_N2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N2.Location = new System.Drawing.Point(24, 20);
this.B_N2.Name = "B_N2";
this.B_N2.Size = new System.Drawing.Size(24, 20);
this.B_N2.TabIndex = 2;
this.B_N2.Text = "2";
this.B_N2.Click += new System.EventHandler(this.B_N2_Click);
//
// B_N1
//
this.B_N1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_N1.Location = new System.Drawing.Point(0, 20);
this.B_N1.Name = "B_N1";
this.B_N1.Size = new System.Drawing.Size(24, 20);
this.B_N1.TabIndex = 1;
this.B_N1.Text = "1";
this.B_N1.Click += new System.EventHandler(this.B_N1_Click);
//
// B_ok
//
this.B_ok.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.B_ok.Location = new System.Drawing.Point(0, 100);
this.B_ok.Name = "B_ok";
this.B_ok.Size = new System.Drawing.Size(96, 20);
this.B_ok.TabIndex = 0;
this.B_ok.Text = "(=)OK";
this.B_ok.Click += new System.EventHandler(this.button2_Click);
//
// TextBoxControl
//
this.Controls.Add(this.panel1);
this.Controls.Add(this.B_1);
this.Controls.Add(this.textBox1);
this.Name = "TextBoxControl";
this.Size = new System.Drawing.Size(114, 144);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextBoxControl_KeyPress);
this.Load += new System.EventHandler(this.TextBoxControl_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region 控件的自定义属性 Hyc07Apr 2005
private string _appVer="1.2";
[CategoryAttribute("版本信息"),
DefaultValueAttribute("1.2"),
DescriptionAttribute("版本信息 Version Information"),
ReadOnlyAttribute(true),
BrowsableAttribute(true)]
public string ControlVer
{
get {return this._appVer;}
set {this._appVer=value;}
}
private string _appname="Text Box Control";
[CategoryAttribute("版本信息"),
DefaultValueAttribute("Text Box Control"),
DescriptionAttribute("控件名称 Control Name"),
ReadOnlyAttribute(true),
BrowsableAttribute(true)]
public string ControlName
{
get {return this._appname;}
set {this._appname=value;}
}
private string Str="";
[CategoryAttribute("自定义属性"),DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),DefaultValueAttribute(""),
ReadOnlyAttribute(false),DescriptionAttribute("Text")]
public new string Text
{
get {return this.Str;}
set {this.Str= value;}
}
#endregion
private static int X;
private static int Y;
public static int B_X
{
set {X=value;}
get {return X;}
}
public static int B_Y
{
set {Y=value;}
get {return Y;}
}
private void TextBoxControl_Load(object sender, System.EventArgs e)
{
panel1.Visible =false;
textBox1.Left =0 ;
textBox1.Width =this.Width -B_1.Width ;
B_1.Left =textBox1.Left +textBox1.Width ;
this.Height =20;
}
private void button1_Click(object sender, System.EventArgs e)
{
Str="";
textBox1.Text=Str;
YunSuanFu="00";
X=B_1.Location .X-panel1.Width ;
Y=B_1.Location .Y+B_1.Height;
panel1.Location =new Point (X,Y);
if(panel1.Visible ==true)
{
panel1.Visible =false;
this.Height =20;
}
else
{
panel1.Visible =true;
this.Height =140;
}
}
#region 小计算器代码
private void Cacul()//计算函数 2005 04 07
{
switch(YunSuanFu)
{
case "1":
if(Str=="")
{Str="0";}
v2=double.Parse (Str);
Result=v1+v2;
Str=Result.ToString ();
textBox1.Text =Str;
break;
case "2":
if(Str=="")
{Str="0";}
v2=double.Parse (Str);
Result=v1-v2;
Str=Result.ToString ();
textBox1.Text =Str;
break;
case "3":
if(Str=="")
{Str="0";}
v2=double.Parse (Str);
Result=v1*v2;
Str=Result.ToString ();
textBox1.Text =Str;
break;
case "4":
if(Str=="")
{Str="0";}
v2=double.Parse (Str);
Result=v1/v2;
Str=Result.ToString ();
textBox1.Text =Str;
break;
default:
break;
}
}
private void button2_Click(object sender, System.EventArgs e)//= OK
{
Cacul();
this.Height =20;
panel1.Visible =false;
}
private void B_N8_Click(object sender, System.EventArgs e)
{
Str=Str+"8";
textBox1.Text =Str;
}
private void B_N3_Click(object sender, System.EventArgs e)
{
Str=Str+"3";
textBox1.Text =Str;
}
private void B_Cancel_Click(object sender, System.EventArgs e)//Clear
{
Str="";
textBox1.Text =Str;
}
private void B_B_Click(object sender, System.EventArgs e)//<--
{
if(Str!="")
{
Str=Str.Substring (0,Str.Length-1);
textBox1.Text =Str;
}
}
private void B_N1_Click(object sender, System.EventArgs e)
{
Str=Str+"1";
textBox1.Text =Str;
}
private void B_N2_Click(object sender, System.EventArgs e)
{
Str=Str+"2";
textBox1.Text =Str;
}
private void B_N4_Click(object sender, System.EventArgs e)
{
Str=Str+"4";
textBox1.Text =Str;
}
private void B_N5_Click(object sender, System.EventArgs e)
{
Str=Str+"5";
textBox1.Text =Str;
}
private void B_N6_Click(object sender, System.EventArgs e)
{
Str=Str+"6";
textBox1.Text =Str;
}
private void B_N7_Click(object sender, System.EventArgs e)
{
Str=Str+"7";
textBox1.Text =Str;
}
private void B_N9_Click(object sender, System.EventArgs e)
{
Str=Str+"9";
textBox1.Text =Str;
}
private void B_N0_Click(object sender, System.EventArgs e)
{
Str=Str+"0";
textBox1.Text =Str;
}
private void B_D_Click(object sender, System.EventArgs e)//.
{
string flag="0";
if(Str=="")
{
Str=Str+"0.";
textBox1.Text =Str;
}
else
{
for(int i=0;i<Str.Length ;i++)
{
if(Str.Substring (i,1)==".")
{
flag="1";
break;
}
}
if(flag=="1")
{
}
else
{
Str=Str+".";
textBox1.Text =Str;
}
}
}
string YunSuanFu;
double v1;
double v2;
double Result;
private void B_J_Click(object sender, System.EventArgs e)//+
{
if(Str=="")
{Str="0";}
Cacul();
DeleteZero();
v1=double.Parse (Str);
Str="";
YunSuanFu="1";
}
private void B_JJ_Click(object sender, System.EventArgs e)//-
{
if(Str=="")
{Str="0";}
Cacul();
DeleteZero();
v1=double.Parse (Str);
Str="";
YunSuanFu="2";
}
private void B_C_Click(object sender, System.EventArgs e)//*
{
if(Str=="")
{Str="0";}
Cacul();
DeleteZero();
v1=double.Parse (Str);
Str="";
YunSuanFu="3";
}
private void B_CC_Click(object sender, System.EventArgs e)//"http://www.cuoxin.com/"
{
if(Str=="")
{Str="0";}
Cacul();
DeleteZero();
v1=double.Parse (Str);
Str="";
YunSuanFu="4";
}
private void DeleteZero()
{
for(int i=1;i<Str.Length ;i++)
{
if(Str.Substring (0,1)=="0" && Str.Substring (1,1)!=".")
{
Str=Str.Substring (1,Str.Length-1 );
textBox1.Text =Str;
}
else
{
break;
}
}
}
#endregion
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
Str=textBox1.Text ;
DeleteZero();
}
private void TextBoxControl_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
}
}