简单的加减乘除实例 C#

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinForm_p169_2
{
        /// <summary>
        /// Form2 的摘要说明。
        /// </summary>
        public class Form2 : System.Windows.Forms.Form
        {
                private System.Windows.Forms.ToolBar toolBar1;
                private System.Windows.Forms.ToolBarButton toolBarButton1;
                private System.Windows.Forms.ToolBarButton toolBarButton2;
                private System.Windows.Forms.ToolBarButton toolBarButton3;
                private System.Windows.Forms.ToolBarButton toolBarButton4;
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Label label2;
                private System.Windows.Forms.TextBox textBox1;
                private System.Windows.Forms.TextBox textBox2;
                private System.Windows.Forms.StatusBar statusBar1;
                private System.Windows.Forms.StatusBarPanel statusBarPanel1;
                private System.Windows.Forms.StatusBarPanel statusBarPanel2;
                int a,b,x; // 运算所需的3个变量
                /// <summary>
                /// 必需的设计器变量。
                /// </summary>
                private System.ComponentModel.Container components = null;

                public Form2()
                {
                        //
                        // Windows 窗体设计器支持所必需的
                        //
                        InitializeComponent();

                        //
                        // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                        //
                }

                /// <summary>
                /// 清理所有正在使用的资源。
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if(components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows 窗体设计器生成的代码
                /// <summary>
                /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                /// 此方法的内容。
                /// </summary>
                private void InitializeComponent()
                {
                        this.toolBar1 = new System.Windows.Forms.ToolBar();
                        this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
                        this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
                        this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
                        this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
                        this.label1 = new System.Windows.Forms.Label();
                        this.label2 = new System.Windows.Forms.Label();
                        this.textBox1 = new System.Windows.Forms.TextBox();
                        this.textBox2 = new System.Windows.Forms.TextBox();
                        this.statusBar1 = new System.Windows.Forms.StatusBar();
                        this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
                        this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
                        ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
                        ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
                        this.SuspendLayout();
                        //
                        // toolBar1
                        //
                        this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                                                                                                                                                                this.toolBarButton1,
                                                                                                                                                                                this.toolBarButton2,
                                                                                                                                                                                this.toolBarButton3,
                                                                                                                                                                                this.toolBarButton4});
                        this.toolBar1.DropDownArrows = true;
                        this.toolBar1.Location = new System.Drawing.Point(0, 0);
                        this.toolBar1.Name = "toolBar1";
                        this.toolBar1.ShowToolTips = true;
                        this.toolBar1.Size = new System.Drawing.Size(292, 41);
                        this.toolBar1.TabIndex = 0;
                        this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
                        //
                        // toolBarButton1
                        //
                        this.toolBarButton1.Tag = "+";
                        this.toolBarButton1.Text = "加";
                        //
                        // toolBarButton2
                        //
                        this.toolBarButton2.Tag = "-";
                        this.toolBarButton2.Text = "减";
                        //
                        // toolBarButton3
                        //
                        this.toolBarButton3.Tag = "*";
                        this.toolBarButton3.Text = "乘";
                        //
                        // toolBarButton4
                        //
                        this.toolBarButton4.Tag = "/";
                        this.toolBarButton4.Text = "除";
                        //
                        // label1
                        //
                        this.label1.Location = new System.Drawing.Point(27, 79);
                        this.label1.Name = "label1";
                        this.label1.Size = new System.Drawing.Size(63, 27);
                        this.label1.TabIndex = 1;
                        this.label1.Text = "第一个数";
                        //
                        // label2
                        //
                        this.label2.Location = new System.Drawing.Point(25, 133);
                        this.label2.Name = "label2";
                        this.label2.Size = new System.Drawing.Size(63, 27);
                        this.label2.TabIndex = 2;
                        this.label2.Text = "第二个数";
                        //
                        // textBox1
                        //
                        this.textBox1.Location = new System.Drawing.Point(94, 77);
                        this.textBox1.Name = "textBox1";
                        this.textBox1.Size = new System.Drawing.Size(129, 21);
                        this.textBox1.TabIndex = 3;
                        this.textBox1.Text = "0";
                        //
                        // textBox2
                        //
                        this.textBox2.Location = new System.Drawing.Point(92, 130);
                        this.textBox2.Name = "textBox2";
                        this.textBox2.Size = new System.Drawing.Size(129, 21);
                        this.textBox2.TabIndex = 4;
                        this.textBox2.Text = "0";
                        //
                        // statusBar1
                        //
                        this.statusBar1.Location = new System.Drawing.Point(0, 251);
                        this.statusBar1.Name = "statusBar1";
                        this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                                                                                                                  this.statusBarPanel1,
                                                                                                                                                                                  this.statusBarPanel2});
                        this.statusBar1.ShowPanels = true;
                        this.statusBar1.Size = new System.Drawing.Size(292, 22);
                        this.statusBar1.TabIndex = 5;
                        this.statusBar1.Text = "statusBar1";
                        //
                        // statusBarPanel1
                        //
                        this.statusBarPanel1.Text = "结果";
                        //
                        // Form2
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                        this.ClientSize = new System.Drawing.Size(292, 273);
                        this.Controls.Add(this.statusBar1);
                        this.Controls.Add(this.textBox2);
                        this.Controls.Add(this.textBox1);
                        this.Controls.Add(this.label2);
                        this.Controls.Add(this.label1);
                        this.Controls.Add(this.toolBar1);
                        this.Name = "Form2";
                        this.Text = "Form2";
                        ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
                        ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
                        this.ResumeLayout(false);

                }
                #endregion
                // 函数验证是否为数字
                public bool IsNumeric(string str)
                {
                        try
                        {
                                // 先判断是否为整形
                                Int32.Parse(str);
                        }
                        catch
                        {
                                // 在判断是否为浮点型
                                try
                                {
                                        Double.Parse(str);
                                }
                                catch
                                {
                                        return false;
                                }
                        }
                        return true;
                }

                // 运算函数
                public void NumOperation(string o)
                {
                        // 判断输入是否为空
                        if((textBox1.Text!=null)||(textBox2.Text!=null))
                        {
                                // 判断是否为数字
                                if(!IsNumeric(textBox1.Text)||!IsNumeric(textBox2.Text))
                                {
                                        this.textBox1.Text="0";
                                        this.textBox2.Text="0";
                                }
                                else
                                {
                                        // 转换输入的2个字符串为数字
                                        a=System.Convert.ToInt32(this.textBox1.Text);
                                        b=System.Convert.ToInt32(this.textBox2.Text);
                                        if(o=="+")
                                                x=a+b;
                                        if(o=="-")
                                                x=a-b;
                                        if(o=="*")
                                                x=a*b;
                                        if(o=="/")
                                                if(b==0)
                                                {
                                                        MessageBox.Show("除数不能为0!");
                                                        this.textBox2.SelectAll();
                                                }
                                                else
                                                        x=a/b;
                                        // 显示结果
                                        statusBarPanel2.Text=x.ToString();
                                }
                        }
                }

                private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
                {
                        // 调用按钮
                        this.NumOperation(e.Button.Tag.ToString());
                }
        }
}

 

作者: Mr.Tang

伯虎

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

7  ×  1  =