我要做一个计数器,可是做到这个MyNumberBox控件,
我的编写的软件就是没有这个控件,该怎么做才有呢?
大家来帮帮忙啊!
MyNumberBox ???
你是想仿照别人的做吧?
那个应该是别人把某个控件改了一个名字而已。
要不就自己写一个控件,呵呵。
是TextBox控件吧!
楼主!
在那里改下名(MyNumberBox)就可以了!
这是我做你,你看下吧!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 计算器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
protected long iNum1;
protected char cOperator;
protected bool bNumBegins;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSubstract;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnEquals;
protected System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.Button button0;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
EventHandler eh = new EventHandler(this.Numbers_Click);
button0.Click += eh;
button1.Click += eh;
button2.Click += eh;
button3.Click += eh;
button4.Click += eh;
button5.Click += eh;
button6.Click += eh;
button7.Click += eh;
button8.Click += eh;
button9.Click += eh;
eh = new EventHandler(this.Operators_Click);
btnAdd.Click += eh;
btnSubstract.Click += eh;
btnMultiply.Click += eh;
btnDivide.Click += eh;
btnEquals.Click += eh;
InitMembers();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitMembers()
{
iNum1 = 0;
cOperator = '=';
bNumBegins = true;
}
private void Numbers_Click(object sender, System.EventArgs e)
{
if(txtOutput.Text == "Error")
{
txtOutput.Text = "0";
}
try
{
long iCurrent = long.Parse(txtOutput.Text);
long i = long.Parse(((Button)sender).Text);
if(bNumBegins)
{
iCurrent = i;
bNumBegins = false;
}
else
{
checked{iCurrent = (iCurrent * 10) + i;}
}
txtOutput.Text = iCurrent.ToString();
}
catch
{
}
}
private void Operators_Click(object sender, System.EventArgs e)
{
char op = ((Button)sender).Text[0];
long iCurrent;
try
{
iCurrent = long.Parse(txtOutput.Text);
}
catch
{
txtOutput.Text = "Error";
InitMembers();
return;
}
long iResult;
try
{
switch(cOperator)
{
case '+':
checked{iResult = iNum1 + iCurrent;}
break;
case '-':
checked{iResult = iNum1 - iCurrent;}
break;
case '*':
checked{iResult = iNum1 * iCurrent;}
break;
case '/':
checked{iResult = iNum1 / iCurrent;}
break;
default:
iResult = iCurrent;
break;
}
}
catch
{
txtOutput.Text = "Error";
InitMembers();
return;
}
txtOutput.Text = iResult.ToString();
iNum1 = iResult;
bNumBegins = true;
// 保存符号
cOperator = op;
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtOutput = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSubstract = new System.Windows.Forms.Button();
this.btnMultiply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnEquals = new System.Windows.Forms.Button();
this.button0 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtOutput
//
this.txtOutput.Location = new System.Drawing.Point(8, 8);
this.txtOutput.MaxLength = 19;
this.txtOutput.Name = "txtOutput";
this.txtOutput.ReadOnly = true;
this.txtOutput.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.txtOutput.Size = new System.Drawing.Size(232, 21);
this.txtOutput.TabIndex = 0;
this.txtOutput.Text = "0";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(8, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 48);
this.button1.TabIndex = 1;
this.button1.Text = "1";
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button2.Location = new System.Drawing.Point(64, 40);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 48);
this.button2.TabIndex = 1;
this.button2.Text = "2";
//
// button3
//
this.button3.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button3.Location = new System.Drawing.Point(120, 40);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 48);
this.button3.TabIndex = 1;
this.button3.Text = "3";
//
// button4
//
this.button4.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button4.Location = new System.Drawing.Point(8, 96);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(48, 48);
this.button4.TabIndex = 1;
this.button4.Text = "4";
//
// button5
//
this.button5.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button5.Location = new System.Drawing.Point(64, 96);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(48, 48);
this.button5.TabIndex = 1;
this.button5.Text = "5";
//
// button6
//
this.button6.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button6.Location = new System.Drawing.Point(120, 96);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(48, 48);
this.button6.TabIndex = 1;
this.button6.Text = "6";
//
// button7
//
this.button7.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button7.Location = new System.Drawing.Point(8, 152);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(48, 48);
this.button7.TabIndex = 1;
this.button7.Text = "7";
//
// button8
//
this.button8.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button8.Location = new System.Drawing.Point(64, 152);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(48, 48);
this.button8.TabIndex = 1;
this.button8.Text = "8";
//
// button9
//
this.button9.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button9.Location = new System.Drawing.Point(120, 152);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(48, 48);
this.button9.TabIndex = 1;
this.button9.Text = "9";
//
// btnAdd
//
this.btnAdd.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnAdd.Location = new System.Drawing.Point(176, 40);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(64, 48);
this.btnAdd.TabIndex = 1;
this.btnAdd.Text = "+";
//
// btnSubstract
//
this.btnSubstract.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnSubstract.Location = new System.Drawing.Point(176, 96);
this.btnSubstract.Name = "btnSubstract";
this.btnSubstract.Size = new System.Drawing.Size(64, 48);
this.btnSubstract.TabIndex = 1;
this.btnSubstract.Text = "-";
//
// btnMultiply
//
this.btnMultiply.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnMultiply.Location = new System.Drawing.Point(176, 152);
this.btnMultiply.Name = "btnMultiply";
this.btnMultiply.Size = new System.Drawing.Size(64, 48);
this.btnMultiply.TabIndex = 1;
this.btnMultiply.Text = "*";
//
// btnDivide
//
this.btnDivide.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnDivide.Location = new System.Drawing.Point(176, 208);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(64, 48);
this.btnDivide.TabIndex = 1;
this.btnDivide.Text = "/";
//
// btnEquals
//
this.btnEquals.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnEquals.Location = new System.Drawing.Point(64, 208);
this.btnEquals.Name = "btnEquals";
this.btnEquals.Size = new System.Drawing.Size(104, 48);
this.btnEquals.TabIndex = 1;
this.btnEquals.Text = "=";
//
// button0
//
this.button0.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button0.Location = new System.Drawing.Point(8, 208);
this.button0.Name = "button0";
this.button0.Size = new System.Drawing.Size(48, 48);
this.button0.TabIndex = 1;
this.button0.Text = "0";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 270);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button4);
this.Controls.Add(this.button5);
this.Controls.Add(this.button6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button8);
this.Controls.Add(this.button9);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnSubstract);
this.Controls.Add(this.btnMultiply);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnEquals);
this.Controls.Add(this.button0);
this.Name = "Form1";
this.Text = "计算器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}