评论

收藏

[C++] C#实验三

编程语言 编程语言 发布于:2021-07-23 17:36 | 阅读数:402 | 评论:0

实验三  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 值,将增加的代码写入实验报告。
实验截图:
DSC0000.png

实验代码:
我把功能全集中在按钮一中,方便编写
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 计算器

{

    public partial class Form1 : Form

    {

        int num1;

        string optType;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

            Console.WriteLine(btn.Text);

            String text = btn.Text;

            if (text == "+" || text == "-" || text == "*" || text == "/"||text == "log")

            { if (textBox1.Text != "")

                {

                    num1 = int.Parse(textBox1.Text);//保存第一个数字

                    textBox1.Text = "";

                    optType = text;

                }

                

           

            }

            else if(text == "X2" || text == "sqrt" || text == "ln")

            {

                optType = text;

            }

            else if(text=="=")

            {

                int num2 = int.Parse(textBox1.Text);

                Double result = 0;

                if(optType=="+")

                {

                    result = num1 + num2;

 

                }

                else if(optType=="-")

                {

                    result = num1 - num2;

                }

                else if (optType=="*")

                {

                    result = num1 * num2;

                }

                else if (optType=="/")

                {

                    if (num2 != 0)

                    {

                        result = num1 / num2;

                    }

               

                    else

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

 

                }

                else if(optType=="X2")

                {

                   

                        result = num2 * num2;

                  

                }

                else if (optType == "sqrt")

                {

                   

                        result = Math.Sqrt(num2);

                  

                }

                else if (optType == "log")

                {

                    if (num1>0)

                    {

                        result = Math.Log(num2,num1);

                    }

                    else

                        MessageBox.Show("log第一个的底必须大于零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

                else if (optType == "ln")

                {

                   

                        result = Math.Log(num2);

                  

                }

                textBox1.Text = result.ToString();

            }

            else if(text=="delete")

            {

                if (textBox1.Text != "")

                {

                    String tempStr = textBox1.Text;

                    tempStr = tempStr.Substring(0, tempStr.Length - 1);

                    textBox1.Text = tempStr;

                }

            }

            else if(text=="clear")

            {

                textBox1.Text = "";

                num1 = 0;

                optType = "";

            }

            else

            {

                string tempStr = textBox1.Text;

                tempStr = tempStr + text;

                textBox1.Text = tempStr;

            }

         }

 

        private void button2_Click(object sender, EventArgs e)

 

        {

           

        }

 

        private void button15_Click(object sender, EventArgs e)

        {

 

        }

 

        private void btn3_Click(object sender, EventArgs e)

        {

           

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            this.btn2.Click += new System.EventHandler(this.button1_Click);

            this.btn3.Click += new System.EventHandler(this.button1_Click);

            this.btn4.Click += new System.EventHandler(this.button1_Click);

            this.btn5.Click += new System.EventHandler(this.button1_Click);

            this.btn6.Click += new System.EventHandler(this.button1_Click);

            this.btn7.Click += new System.EventHandler(this.button1_Click);

            this.btn8.Click += new System.EventHandler(this.button1_Click);

            this.btn9.Click += new System.EventHandler(this.button1_Click);

            this.btn0.Click += new System.EventHandler(this.button1_Click);

            this.buttonjian.Click += new System.EventHandler(this.button1_Click);

            this.buttonjia.Click += new System.EventHandler(this.button1_Click);

            this.clear.Click += new System.EventHandler(this.button1_Click);

            this.buttonjisuan.Click += new System.EventHandler(this.button1_Click);

            this.buttondelete.Click += new System.EventHandler(this.button1_Click);

            this.buttonchu.Click += new System.EventHandler(this.button1_Click);

            this.buttonchen.Click += new System.EventHandler(this.button1_Click);

            this.buttonX2.Click += new System.EventHandler(this.button1_Click);

            this.buttonsqrt.Click += new System.EventHandler(this.button1_Click);

            this.buttonlog.Click += new System.EventHandler(this.button1_Click);

            this.buttonln.Click += new System.EventHandler(this.button1_Click);

        }

 

        private void btn4_Click(object sender, EventArgs e)

        {

           

        }

 

        private void btn5_Click(object sender, EventArgs e)

        {

          

        }

 

        private void btn6_Click(object sender, EventArgs e)

        {

          

        }

 

        private void btn7_Click(object sender, EventArgs e)

        {

           

        }

 

        private void btn8_Click(object sender, EventArgs e)

        {

           

        }

 

        private void btn9_Click(object sender, EventArgs e)

        {

         

        }

 

        private void btn0_Click(object sender, EventArgs e)

        {

           

        }

 

        private void buttonchu_Click(object sender, EventArgs e)

        {

 

        }

 

        private void button1_Click_1(object sender, EventArgs e)

        {

 

        }

    }

}

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

DSC0002.png

在右边的填写会在左边进行显示;
实验源码:
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 简单应用程序

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox2.Text != "") { label8.Text = textBox2.Text; }                                  //姓名

            else MessageBox.Show("姓名不能为空!\n");

            if (radioButton1.Checked) { label9.Text = radioButton1.Text; } //男

            if (radioButton2.Checked) { label9.Text = radioButton2.Text; } //女

            if (checkBox1.CheckState == CheckState.Checked) { label20.Text = checkBox1.Text; }//看书

            else label20.Text = "";

            if (checkBox2.CheckState == CheckState.Checked) { label21.Text = checkBox2.Text; }//编程

            else label21.Text = "";

            if (checkBox3.CheckState == CheckState.Checked) { label22.Text = checkBox3.Text; }//羽毛球

            else label22.Text = "";

            if (checkBox3.CheckState == CheckState.Checked) { label23.Text = checkBox4.Text; }//羽毛球

            else label23.Text = "";

            if (checkBox1.CheckState == CheckState.Unchecked && checkBox2.CheckState == CheckState.Unchecked && checkBox3.CheckState == CheckState.Unchecked && checkBox4.CheckState == CheckState.Unchecked) { MessageBox.Show("选个爱好吧!\n"); }

            if (comboBox1.Text != "") { label11.Text = comboBox1.Text; }//班级

            else MessageBox.Show("班级不能为空!\n");

            if (listBox1.Text != "") { label12.Text = listBox1.Text; } //座右铭

            else MessageBox.Show("选个座右铭吧!\n");

        }

    }

}

四、实验总结
本次实验对c#的语法,以及控件的使用,有了更加深刻的认识
熟悉了对控件,name以及text以及其他的一些属性的了解,对代码有了更加深入的了解。
中间遇到,修改前端代码,会自动检索后端的代码的关键字然后同步修改,某些时候会造成不必要的错误;
然后就是textbox控件无法设为透明显示,最后用label实现了相同的效果。


关注下面的标签,发现更多相似文章