首页 > 开发 > 综合 > 正文

带有小计算器的TextBox控件

2024-07-21 02:24:18
字体:
来源:转载
供稿:网友

/*[email protected]

需要的添加的控件,  

  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 = "/";
   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)//"/"
  {
   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)
  {
  
  }
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表