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 WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Text = "If语法计算";
button2.Text = "switch语法计算";
label2.Text = "省";
label3.Text = "市";
}
private void Button1_Click(object sender, EventArgs e)
{
int text1 = Convert.ToInt32(textBox1.Text);
int text2 = Convert.ToInt32(textBox2.Text);
int text3 = 0; //用if的时候需要对变量进行赋值,不然在if语句里会报错
if (comboBox1.Text == "+" && comboBox1.SelectedItem == "+")
//由上一行可以看出combox的text和selecteditem的值一样,
//但是建议使用后者,所以好的写法如下面的
{
text3 = text1 + text2;
}
else if (comboBox1.Text == "-")
{
text3 = text1 - text2;
}
else if (comboBox1.Text == "*")
{
text3 = text1 * text2;
}
else if (comboBox1.Text == "/")
{
if (text2 == 0)
{
MessageBox.Show("除数不能为0");
return;
}
else
{
text3 = text1 / text2;
}
}
else
{
MessageBox.Show("不知道该怎么计算");
}
textBox3.Text = Convert.ToString(text3);
}
private void Button2_Click(object sender, EventArgs e)
{
try
{
int text1 = Convert.ToInt32(textBox1.Text);
int text2 = Convert.ToInt32(textBox2.Text);
int text3;//switch时候就不需要先对变量赋值
switch (comboBox1.SelectedItem)
{
case "+":
text3 = text1 + text2;
break;
case "-":
text3 = text1 - text2;
break;
case "*":
text3 = text1 * text2;
break;
case "/":
//if (text2 == 0)
//{
// MessageBox.Show("除数不能为0");
// return;
//}
text3 = text1 / text2;
break;
default:
MessageBox.Show("不知道该怎么计算");
return;
//throw new Exception("不知道怎么计算");也可以用这个
}
textBox3.Text = Convert.ToString(text3);
}
catch (Exception ex)
{
MessageBox.Show("程序出现意外" + ex.Message);
}
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedItem == "江苏省")
{
comboBox3.Items.Clear();//每次选择省时,清空市的内容
comboBox3.Items.Add("苏州");
comboBox3.Items.Add("张家港");
comboBox3.Items.Add("昆山");
comboBox3.Items.Add("吴江");
}
if (comboBox2.SelectedItem == "山东省")
{
comboBox3.Items.Clear();
comboBox3.Items.Add("青岛1");
comboBox3.Items.Add("青岛2");
comboBox3.Items.Add("青岛3");
comboBox3.Items.Add("青岛4");
}
if (comboBox2.SelectedItem == "浙江省")
{
comboBox3.Items.Clear();
comboBox3.Items.Add("杭州");
comboBox3.Items.Add("义乌");
comboBox3.Items.Add("温州");
comboBox3.Items.Add("台州");
}
}
private void ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
//初始化月份的下拉选择项
{
comboBox4.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox4.Items.Clear();
for (int i = 1; i < 13; i++)
{
//comboBox4.Items.Add(i.ToString());//这个也是类型转换
comboBox4.Items.Add(Convert.ToString(i));
}
}
private void ComboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
switch (Convert.ToInt32(comboBox4.SelectedItem))
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
comboBox5.Items.Clear();
for (int i = 1; i < 32; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
case 2:
comboBox5.Items.Clear();
for (int i = 1; i < 30; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
default:
comboBox5.Items.Clear();
for (int i = 1; i < 31; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
}
}
private void Button3_Click(object sender, EventArgs e)
{
string str = string.Format("你选择了{0}月{1}日", comboBox4.SelectedItem, comboBox5.SelectedItem);
MessageBox.Show(str);
string str1 = comboBox4.SelectedItem + "月" + comboBox5.SelectedItem + "日";
MessageBox.Show(str1);
}
}
}

总结
整个的设计与实现过程中,收获颇多。首先就是我发现在实现之前能够有一个比较良好的设计思路以及程序流程的重要性(最好是能够记录下来而不是存在于脑海中的)。具体能够反映上述情况的有如下两个方面:我在第一周实现的是控制台的程序,在第二周才完成了移植到MFC上的工作,在实现控制台程序之前我对程序整体的设计有一个比较具体的构思,所以最后实现出来的程序的代码结构我自认为还是比较良好的,然后在第二周移植到MFC的过程中,在事先我没有很具体的想程序的实现结构,而是只有一个大概的结构。这就导致了我在编写程序的过程中不断的发现问题,然后就调整实现的过程,这样就使我最后MFC程序的代码结构以及函数调用之间的关系比较混乱,说实话有一些时候我也不是第一时间就能看懂我写的代码;第二一点就是,由于我在第一周早些时候就已经完成了控制台程序的编写,而在第二周晚些时候才开始做MFC程序。在写MFC的过程中又涉及到修改之前的函数实现(char转cstring),然后我就有点看不懂之前写的码了,所以深刻感觉到这时候如果有能够记录下来的设计思路是多么重要。第二个收获就是在程序实现的最开始一定要有一个目标,要对未来程序的扩展有预先的设想,并能够留下充足的接口以备日后进行修改。我在写控制台程序时就没有考虑到这重情况,导致后面在实现MFC时修改了很多代码以适应MFC,所以我从中了解到在事先将事情考虑的长远一些事多么重要。
原文:https://www.cnblogs.com/yxd1102/p/14645300.html