我是靠谱客的博主 调皮大神,最近开发中收集的这篇文章主要介绍.net 实验三 Windows 应用程序开发 (1),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、实验目的

1. 掌握窗口控件的使用方法;

2. 掌握 Windows 的编程基础。

二、实验要求

根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告。

三、实验内容

1.编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性, 编写事件处理程序的方法。

(1)新建 windows 应用程序。在窗体 Form 上拖放一个 TextBox 控件、十六个 Button 控

件,整个窗体布局如下图所示。

(2)打开代码窗口,添加如下全局变量:

double a = 0;

double b = 0;

bool c = false;

string d;

  1. 双击”1”按钮,添加如下事件处理程序:

private void button1_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "1";

}

(4)双击”2”按钮,添加如下事件处理程序:

private void button2_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox2.Text = "";

c = false;

}

textBox1.Text += "2";

}

(5)双击”3”按钮,添加如下事件处理程序:

private void button3_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox3.Text = "";

c = false;

}

textBox1.Text += "3";

}

(6)双击”4”按钮,添加如下事件处理程序:

private void button4_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "4";

}

(7)双击”5”按钮,添加如下事件处理程序:

private void button5_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "5";

}

(8)双击”6”按钮,添加如下事件处理程序:

private void button6_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "6";

}

(8)双击”7”按钮,添加如下事件处理程序:

private void button7_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "7";

}

(10)双击”8”按钮,添加如下事件处理程序:

private void button8_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "8";

}

(11)双击”9”按钮,添加如下事件处理程序:

private void button9_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "9";

}

(12)双击”0”按钮,添加如下事件处理程序:

private void button12_Click(object sender, EventArgs e)

{

if (c == true)

{

textBox1.Text = "";

c = false;

}

textBox1.Text += "0";

if (d == "/")

{

textBox1.Clear();

MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,

MessageBoxIcon.Warning);

}

}

(13)双击”+”按钮,添加如下事件处理程序:

private void button13_Click(object sender, EventArgs e)

{

c = true;

b = double.Parse(textBox1.Text);

d = "+";

}

(14)双击”-”按钮,添加如下事件处理程序:

private void button16_Click(object sender, EventArgs e)

{

c = true;

b = double.Parse(textBox1.Text);

d = "-";

}

(15)双击”*”按钮,添加如下事件处理程序:

private void button15_Click(object sender, EventArgs e)

{

c = true;

b = double.Parse(textBox1.Text);

d = "*";

}

(16)双击”/”按钮,添加如下事件处理程序:

private void button14_Click(object sender, EventArgs e)

{

c = true;

b = double.Parse(textBox1.Text);

d = "/";

}

(17)双击”=”按钮,添加如下事件处理程序:

private void button17_Click(object sender, EventArgs e)

{

switch (d)

{

case "+": a = b + double.Parse(textBox1.Text); break;

case "-": a = b - double.Parse(textBox1.Text); break;

case "*": a = b * double.Parse(textBox1.Text); break;

case "/": a = b / double.Parse(textBox1.Text); break;

}

textBox1.Text = a + "";

c = true;

}

(18)双击”c”按钮,添加如下事件处理程序:

private void button18_Click(object sender, EventArgs e)

{

textBox1.Text = "";

}

(19)单击启动调试工具,运行计算器。

(20)在计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方,

log,ln 值,将增加的代码写入实验报告。

2.自己设计并编写一个 Windows 应用程序,要求用到 TextBox、GroupBox、 RadioButton、CheckBox、ComboBox、ListBox 控件、Timer 控件。将程序功能、界面布 局和运行结果的截图与事件代码写在实验报告中。

Form1.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 崔金泽_20192163
{

    public partial class 计算器 : Form
    {
        double a = 0;
        double b = 0;
        bool c = false;
        string d;
        public 计算器()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "0";
            if (d == "/")
            {
                textBox1.Clear();
                MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,
               MessageBoxIcon.Warning);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "1";

        }

        private void button13_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "/";

        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "7";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "3";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "4";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "5";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "6";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "8";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                textBox1.Text = "";
                c = false;
            }
            textBox1.Text += "9";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "+";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "-";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "*";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            switch (d)
            {
                case "+": a = b + double.Parse(textBox1.Text); break;
                case "-": a = b - double.Parse(textBox1.Text); break;
                case "*": a = b * double.Parse(textBox1.Text); break;
                case "/": a = b / double.Parse(textBox1.Text); break;
                case "X": a = b * b; break;
                case "sqrt": a = Math.Sqrt(b); break;
                case "log": a = Math.Log(b, double.Parse(textBox1.Text)); break;//第二个参数是底
                case "ln": a = Math.Log(b); break;//只一个参数默认以自然对数为底

            }
            textBox1.Text = a + "";
            c = true;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "X";

        }

        private void button20_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "log";
            
        }

        private void button19_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "sqrt";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            c = true;
            b = double.Parse(textBox1.Text);
            d = "ln";
        }
    }
}

Program.cs的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 崔金泽_20192163
{

    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new 计算器());
        }
    }
}

Form1.Designers.cs

namespace 20192163
{
    partial class 计算器
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = 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.button10 = new System.Windows.Forms.Button();
            this.button11 = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button15 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button17 = new System.Windows.Forms.Button();
            this.button18 = new System.Windows.Forms.Button();
            this.button19 = new System.Windows.Forms.Button();
            this.button20 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(110, 21);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(209, 27);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(78, 180);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(41, 34);
            this.button2.TabIndex = 2;
            this.button2.Text = "1";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(240, 124);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(41, 34);
            this.button1.TabIndex = 3;
            this.button1.Text = "6";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(240, 180);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(41, 34);
            this.button3.TabIndex = 4;
            this.button3.Text = "3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(240, 234);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(41, 34);
            this.button4.TabIndex = 5;
            this.button4.Text = "+";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(240, 74);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(41, 34);
            this.button5.TabIndex = 6;
            this.button5.Text = "9";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(160, 74);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(41, 34);
            this.button6.TabIndex = 7;
            this.button6.Text = "8";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(78, 74);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(41, 34);
            this.button7.TabIndex = 8;
            this.button7.Text = "7";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.button7_Click);
            // 
            // button8
            // 
            this.button8.Location = new System.Drawing.Point(160, 234);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(41, 34);
            this.button8.TabIndex = 9;
            this.button8.Text = "C";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button8_Click);
            // 
            // button9
            // 
            this.button9.Location = new System.Drawing.Point(160, 180);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(41, 34);
            this.button9.TabIndex = 10;
            this.button9.Text = "2";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button9_Click);
            // 
            // button10
            // 
            this.button10.Location = new System.Drawing.Point(160, 124);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(41, 34);
            this.button10.TabIndex = 11;
            this.button10.Text = "5";
            this.button10.UseVisualStyleBackColor = true;
            this.button10.Click += new System.EventHandler(this.button10_Click);
            // 
            // button11
            // 
            this.button11.Location = new System.Drawing.Point(78, 234);
            this.button11.Name = "button11";
            this.button11.Size = new System.Drawing.Size(41, 34);
            this.button11.TabIndex = 12;
            this.button11.Text = "0";
            this.button11.UseVisualStyleBackColor = true;
            this.button11.Click += new System.EventHandler(this.button11_Click);
            // 
            // button12
            // 
            this.button12.Location = new System.Drawing.Point(78, 124);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(41, 34);
            this.button12.TabIndex = 13;
            this.button12.Text = "4";
            this.button12.UseVisualStyleBackColor = true;
            this.button12.Click += new System.EventHandler(this.button12_Click);
            // 
            // button13
            // 
            this.button13.Location = new System.Drawing.Point(328, 74);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(41, 34);
            this.button13.TabIndex = 17;
            this.button13.Text = "÷";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.button13_Click);
            // 
            // button14
            // 
            this.button14.Location = new System.Drawing.Point(328, 234);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(41, 34);
            this.button14.TabIndex = 16;
            this.button14.Text = "=";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.button14_Click);
            // 
            // button15
            // 
            this.button15.Location = new System.Drawing.Point(328, 180);
            this.button15.Name = "button15";
            this.button15.Size = new System.Drawing.Size(41, 34);
            this.button15.TabIndex = 15;
            this.button15.Text = "-";
            this.button15.UseVisualStyleBackColor = true;
            this.button15.Click += new System.EventHandler(this.button15_Click);
            // 
            // button16
            // 
            this.button16.Location = new System.Drawing.Point(328, 124);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(41, 34);
            this.button16.TabIndex = 14;
            this.button16.Text = "X";
            this.button16.UseVisualStyleBackColor = true;
            this.button16.Click += new System.EventHandler(this.button16_Click);
            // 
            // button17
            // 
            this.button17.Location = new System.Drawing.Point(328, 294);
            this.button17.Name = "button17";
            this.button17.Size = new System.Drawing.Size(41, 34);
            this.button17.TabIndex = 21;
            this.button17.Text = "ln";
            this.button17.UseVisualStyleBackColor = true;
            this.button17.Click += new System.EventHandler(this.button17_Click);
            // 
            // button18
            // 
            this.button18.Location = new System.Drawing.Point(78, 295);
            this.button18.Name = "button18";
            this.button18.Size = new System.Drawing.Size(52, 34);
            this.button18.TabIndex = 20;
            this.button18.Text = "x^2";
            this.button18.UseVisualStyleBackColor = true;
            this.button18.Click += new System.EventHandler(this.button18_Click);
            // 
            // button19
            // 
            this.button19.Location = new System.Drawing.Point(160, 295);
            this.button19.Name = "button19";
            this.button19.Size = new System.Drawing.Size(51, 34);
            this.button19.TabIndex = 19;
            this.button19.Text = "sqrt";
            this.button19.UseVisualStyleBackColor = true;
            this.button19.Click += new System.EventHandler(this.button19_Click);
            // 
            // button20
            // 
            this.button20.Location = new System.Drawing.Point(240, 295);
            this.button20.Name = "button20";
            this.button20.Size = new System.Drawing.Size(41, 34);
            this.button20.TabIndex = 18;
            this.button20.Text = "log";
            this.button20.UseVisualStyleBackColor = true;
            this.button20.Click += new System.EventHandler(this.button20_Click);
            // 
            // 计算器
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(461, 359);
            this.Controls.Add(this.button17);
            this.Controls.Add(this.button18);
            this.Controls.Add(this.button19);
            this.Controls.Add(this.button20);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button15);
            this.Controls.Add(this.button16);
            this.Controls.Add(this.button12);
            this.Controls.Add(this.button11);
            this.Controls.Add(this.button10);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.textBox1);
            this.Name = "计算器";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
        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 button10;
        private System.Windows.Forms.Button button11;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button19;
        private System.Windows.Forms.Button button20;
    }
}

运行结果:

最后

以上就是调皮大神为你收集整理的.net 实验三 Windows 应用程序开发 (1)的全部内容,希望文章能够帮你解决.net 实验三 Windows 应用程序开发 (1)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部