.NET程序设计实验三

实验三  Windows 应用程序开发

一、实验目的

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

2. 掌握Windows 的编程基础。

二、实验要求

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

三、实验内容

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

编写事件处理程序的方法。

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

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

 

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

  double a = 0; 

  double b = 0;

  bool c = false;

  string d;

(3)双击”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 值,将增加的代码写入实验报告。

增加的代码

private void buttonfang_Click(object sender, EventArgs e)

        {

            c = true;

 

            b = double.Parse(textBox1.Text);

 

            d = "x2";

        }

 

        private void buttonkai_Click(object sender, EventArgs e)

        {

            c = true;

 

            b = double.Parse(textBox1.Text);

 

            d = "sqrt";

        }

 

        private void buttonlog_Click(object sender, EventArgs e)

        {

            c = true;

 

            b = double.Parse(textBox1.Text);

 

            d = "log";

        }

 

        private void buttonln_Click(object sender, EventArgs e)

        {

            c = true;

 

            b = double.Parse(textBox1.Text);

 

            d = "ln";

        }

运行结果:

加法

 

 

减法

 

 

乘法

 

 

除法

 

平方

 

 

开方

 

 

对数

 

 

自然对数

 

 

 

2.自己设计并编写一个 Windows 应用程序,要求用到 TextBox、GroupBox、

RadioButton、CheckBox、ComboBox、ListBox 控件。将程序功能、界面布局和运行结果

的截图与事件代码写在实验报告中。

截图:

 

代码:

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 WinFormsApp2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            insertComboItem();

        }

 

        private void groupBox1_Enter(object sender, EventArgs e)

        {

 

        }

 

        private void label1_Click(object sender, EventArgs e)

        {

 

        }

 

        private void insertComboItem()

        {

            comboBox1.Items.Add("信息学院");

            comboBox1.Items.Add("经管学院");

            comboBox1.Items.Add("机械学院");

            comboBox1.Items.Add("文法学院");

 

            //默认选择第一项

            comboBox1.SelectedIndex = 0;

        }

 

        private void label4_Click(object sender, EventArgs e)

        {

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Clear();

            radioButton1.Checked = false;

            radioButton2.Checked = false;

            comboBox1.SelectedIndex = 0;

            checkBox1.Checked = false;

            checkBox2.Checked = false;

            listBox1.ClearSelected();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string radio_str = " ";

            string check_str = " ";

            string text_str = textBox1.Text;

            string combo_str = comboBox1.Text;

            string list_str = " ";

 

            if (radioButton1.Checked==true) {

                radio_str = "男";

            } else {

                radio_str = "女";

            }

 

            for (int i = 0; i < this.groupBox1.Controls.Count; i++)

            {

                var cb = this.groupBox1.Controls[i] as CheckBox;

                if (cb.Checked == true)

                {

                    check_str += cb.Text.ToString() + " ";

                }

            }

 

            for (int i = 0; i < listBox1.SelectedItems.Count; i++)

            {

                list_str += listBox1.SelectedItems[i].ToString()+" ";

            }

 

            MessageBox.Show("姓名:" + text_str + "\n" +

                            "性别:" + radio_str + "\n" +

                            "学院:" + combo_str + "\n" +

                            "电子设备:" + check_str + "\n" +

                             "爱好:" + list_str

                            );

 

 

        }

 

        private void radioButton1_CheckedChanged(object sender, EventArgs e)

        {

            if (radioButton1.Checked == true)

            {

               

             }

        }

 

        private void radioButton2_CheckedChanged(object sender, EventArgs e)

        {

 

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

          

        }

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

 

        }

 

        private void checkBox1_CheckedChanged(object sender, EventArgs e)

        {

 

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

 

        }

    }

}

 

四、实验总结

注:本部分写本次实验过程中出现的问题、如何解决、注意事项、以及自己的经 验体会。

这个计算机的整体运算逻辑比较简单,用两个变量来存储前后输入的值,根据选择的符号来进行运算,但是功能还是太过于简单,如果想要像平常用的计算机那样,就要更换数据的储存和计算模式了,可以将前两个数值加上运算符先运算出来,然后充当下一次计算的第一个数值。

 

posted @ 2021-10-24 23:43  我试试这个昵称好使不  阅读(130)  评论(0)    收藏  举报