刘一辰的软件工程随笔
实验三 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 值,将增加的代码写入实验报告。
2.自己设计并编写一个 Windows 应用程序,要求用到 TextBox、GroupBox、
RadioButton、CheckBox、ComboBox、ListBox 控件。将程序功能、界面布局和运行结果
的截图与事件代码写在实验报告中。
private void button17_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
a = b * b;
textBox1.Text = a + "";
c = true;
}
private void button18_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
a = Math.Sqrt(b);
textBox1.Text = a + "";
c = true;
}
private void button19_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
a = Math.Log(b, 10);
textBox1.Text = a + "";
c = true;
}
private void button20_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
a = Math.Log(b, Math.E);
textBox1.Text = a + "";
c = true;
}
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 text
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (ischecked("checkBox1")==true)
{
ListBox i=(ListBox)Controls.Find("listBox1", true)[0];
RadioButton a = (RadioButton)Controls.Find("radioButton1", true)[0];
RadioButton b = (RadioButton)Controls.Find("radioButton2", true)[0];
RadioButton c = new RadioButton();
if (a.Checked==true)
{
c = a;
}
else
{
c = b;
}
i.Items.Add(textBox1.Text+" "+ comboBox1.Text+" "+c.Text);
MessageBox.Show("提交成功");
}
else
{
MessageBox.Show("请勾选确定");
}
}
public bool ischecked(string str)
{
foreach (Control item in Controls)
{
if (item is GroupBox)
{
foreach (Control i in item.Controls)
{
if (i.Name.Equals(str))
{
if(((CheckBox)i).Checked == true)
{
return true;
}
else
{
return false;
}
}
}
}
}
return false;
}
private void button2_Click(object sender, EventArgs e)
{
ListBox i = (ListBox)Controls.Find("listBox1", true)[0];
i.Items.Clear();
}
}
}
四、实验总结
学习了find的用法和遍历groupbox里的控件 获取控件值的操作已经熟练 明白了control
向其他控件的转换
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!