Windows 应用程序开发

发布时间 2023-12-18 11:23:53作者: YE-

实验三 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 button17_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text = "" + Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox1.Text);
}
}

增加sqrt代码:
private void button18_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text = "" + System.Math.Sqrt(Convert.ToInt32(textBox1.Text));
}

增加log代码:log3 9

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

    case "log": a = Math.Log(b, Convert.ToInt32(textBox1.Text)); break;

增加ln代码:ln5
private void button20_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text = "" + Math.Log(Convert.ToInt32(textBox1.Text), 2.718281828459);
}

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 firstWinform
    {

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
listBox1.Items.Add("除了靠谱,我什么都给不了你。\n");
comboBox1.Items.Add("信1905-1");//选择项1
comboBox1.Items.Add("信1905-2");
}

private void Form2_Load(object sender, EventArgs e)
{

    }

private void label1_Click(object sender, EventArgs e)
{

    }

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    }

private void button1_Click(object sender, EventArgs e)
{
label6.Text = textBox1.Text;
if (radioButton1.Checked) { label7.Text = radioButton1.Text; } //男
if (radioButton2.Checked) { label7.Text = radioButton2.Text; } //女
if (checkBox1.CheckState == CheckState.Checked) { label8.Text = label8.Text + checkBox1.Text; }
if (checkBox2.CheckState == CheckState.Checked) { label8.Text = label8.Text + checkBox2.Text; }
if (checkBox3.CheckState == CheckState.Checked) { label8.Text = label8.Text + checkBox3.Text; }
if (comboBox1.Text != "") { label9.Text = comboBox1.Text; }//班级
else MessageBox.Show("班级为必填项!!\n");
if (listBox1.Text != "") { label10.Text = listBox1.Text; } //座右铭
else MessageBox.Show("请填写个人简介!\n");

    }
    }
    }

四、实验总结
注:本部分写本次实验过程中出现的问题、如何解决、注意事项、以及自己的经验体会。
1、 每一个控件和方法是一一对应的,注意先设置控件的属性,然后再写对应的方法,控件的name要命名规范。
2、 在布局界面的时候,要考虑到方法和控件的顺序问题,比如计算器的1、2、3等等控件,最好按顺序去写对应的方法。
3、 第二题,掌握了C#窗体应用程序简单的文本框、label框、单选框、复选框、下拉框的简单使用。