实验名称:实验三 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)单击启动调试工具,运行计算器。

 

    单击9  单击*   单击2

 

    单击 =

(20)在计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方,log,ln 值,将增加的代码写入实验报告。

1)X2功能实现:

private void button16_Click(object sender, EventArgs e)

        {

            a = double.Parse(textBox1.Text) *  double.Parse(textBox1.Text);

            textBox1.Text = a + "";

            c = true;

        }

 

单击6单击X2

2sqrt功能实现:

private void button15_Click(object sender, EventArgs e)

        {

            a = Math.Pow(double.Parse(textBox1.Text), 1 / 2);

            textBox1.Text = a + "";

            c = true;

        }

 

  单击9 单击sqrt

3log功能实现:

private void button4_Click(object sender, EventArgs e)

        {

            a = Math.Log(double.Parse(textBox1.Text));

            textBox1.Text = a + "";

            c = true;

        }

  

依此单击625单击log 单击4

 

计算结果为4

4)ln功能实现:

private void button4_Click(object sender, EventArgs e)

        {

            a = Math.Log(double.Parse(textBox1.Text));

            textBox1.Text = a + "";

            c = true;

        }

 

输入55    计算结果约为4

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

 

1) 程序功能:

将选择或录入的个人信息保存在程序中,并在录入完成并按下完成按钮后,以提示框的形式回馈给用户。

2) 界面布局:

 

实现界面

3) 运行结果(仅为示例):

 

 

4) 事件代码(以下为全部代码,内含各种事件代码):

 

i.Form1.cs文件代码:

using System;

using System.Collections;

using System.Threading;

using System.Windows.Forms;

using static System.Windows.Forms.VisualStyles.VisualStyleElement;

using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

using Timer = System.Threading.Timer;

 

 

namespace WinFormsApp1

{

    public partial class Form1 : Form

    {

        public string A = "";

        int Ti = 0;

 

        public string name;

        string sentence = "";

        string sex = "";

        string[] hobbies = {"","","",""};   //唱、跳、rap、篮球

        bool flag1=false;                   //判断“唱”是否被选中

        bool flag2=false;                   //判断“跳”是否被选中

        bool flag3=false;                   //判断rap”是否被选中

        bool flag4=false;                   //判断“篮球”是否被选中

        string education = "";

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            Ti++;

            A = "第"+Ti.ToString()+"次显示";

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

            name = textBox1.Text;

        }

 

        private void label1_Click(object sender, EventArgs e)

        {

            label1.Text = A;

            A = "你的姓名是:" + name + "\n" + "你的座右铭是:" + sentence + "\n" + "你的性别是:" + sex + "\n" + "你的爱好有:" + hobbies[0] + hobbies[1] + hobbies[2] + hobbies[3] +"\n"+"你的最高学历是:"+education+"\n";

            MessageBox.Show(A);

    }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            education = comboBox1.Text;

        }

 

        private void label3_Click(object sender, EventArgs e)

        {

 

        }

 

        private void checkBox1_CheckedChanged(object sender, EventArgs e)

        {

            if (flag4 == false)

            {

                flag4 = true;

                hobbies[3] = "篮球";

            }

            else

            {

                flag4 = false;

                hobbies[3] = "";

            }

        }

 

        private void groupBox1_Enter(object sender, EventArgs e)

        {

            

        }

 

        private void 此处撰写_SelectedIndexChanged(object sender, EventArgs e)

        {

            

        }

 

        private void radioButton2_CheckedChanged(object sender, EventArgs e)

        {

            if (radioButton2.Checked)

            {

                sex = "女";

            }

        }

 

        private void radioButton1_CheckedChanged(object sender, EventArgs e)

        {

            if (radioButton1.Checked)

            {

                sex = "男";

            }

        }

 

        private void checkBox3_CheckedChanged(object sender, EventArgs e)

        {

            if (flag2 == false)

            {

                flag2 = true;

                hobbies[1] = "跳";

            }

            else

            {

                flag2 = false;

                hobbies[1] = "";

            }

        }

 

        private void checkBox2_CheckedChanged(object sender, EventArgs e)

        {

            if (flag1 == false)

            {

                flag1 = true;

                hobbies[0] = "唱";

            }

            else

            {

                flag1 = false;

                hobbies[0] = "";

            }

        }

 

        private void checkBox4_CheckedChanged(object sender, EventArgs e)

        {

            if (flag3 == false)

            {

                flag3 = true;

                hobbies[2] = "rap";

            }

            else

            {

                flag3 = false;

                hobbies[2] = "";

            }

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

 

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

            {

                //行被选中时

                if (listBox1.SelectedItems.Contains(listBox1.Items[i]))

                {

                    if (listBox1.Items[i].ToString() == null)

                    {

                        sentence = "未选择";

                    }

                    else

                    {

                        sentence = listBox1.Items[i].ToString();

                    }

                    

                }

            }

        }

 

        private void label6_Click(object sender, EventArgs e)

        {

 

        }

    }

}

ii. Program.cs文件代码

namespace WinFormsApp1

{

    internal static class Program

    {

        /// <summary>

        ///  The main entry point for the application.

        /// </summary>

        

        [STAThread]

        static void Main()

        {

            ApplicationConfiguration.Initialize();

            Application.Run(new Form1());

        }

    }

}

iii.设计界面

 

posted on 2022-11-03 19:53  辰逸1  阅读(109)  评论(0编辑  收藏  举报