- 本篇博客所讲内容围绕Microsoft Visual Studio 2010开发工具,其他版本的相关操作略有不同,但是大同小异
- 本篇博客的作用只是作为记录知识点的作用,不涉及软件工程问题
窗口使用
1、添加控件
一般打开项目中的窗口类就会自动打开工具箱,如上图所示,把工具箱里的控件拖到窗口中即可。
2、修改、添加控件属性或事件
先点击窗口中的控件,然后会默认进行“属性”窗口,如上图所示。注意属性窗口上方的两个红框,第一个红框(和本子差不多的图标)是属性,可自行选中属性进行修改,第二个红框(和闪电差不多的图标)是事件。选中事件后,双击就进行编程序的界面。
项目结构
- Form1.css是窗口类,包括前台设计和后台代码
- Program.cs 是程序入口
- 从设计界面到代码界面: 在设计界面上右键——查看代码
另外需要注意的是,如果不小心把某窗口关了,例如把控件的属性关了。
可以左侧图片所述位置添加所需窗口。
后台代码
1、Form加载事件:
即窗口才出现时执行的方法。双击窗口即可。
2、事件代码:
例如,给button添加点击监听事件
小案例:
采集个人信息,效果如下:
前台设计部分代码(主要看每个控件的name):
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox tb_stuno;
private System.Windows.Forms.TextBox tb_name;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.ComboBox cb_collage;
private System.Windows.Forms.ComboBox cb_zhuanye;
private System.Windows.Forms.ComboBox cb_grade;
private System.Windows.Forms.ComboBox cb_class;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox gb_sex;
private System.Windows.Forms.RadioButton rb_women;
private System.Windows.Forms.RadioButton rb_man;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.ComboBox cb_day;
private System.Windows.Forms.ComboBox cb_mouth;
private System.Windows.Forms.ComboBox cb_year;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.GroupBox gb_habit;
private System.Windows.Forms.CheckBox checkBox6;
private System.Windows.Forms.CheckBox checkBox5;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox7;
private System.Windows.Forms.CheckBox checkBox8;
private System.Windows.Forms.CheckBox checkBox9;
private System.Windows.Forms.CheckBox checkBox10;
private System.Windows.Forms.CheckBox checkBox11;
private System.Windows.Forms.CheckBox checkBox12;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.ListBox lb_base;
private System.Windows.Forms.Button bt_addpart;
private System.Windows.Forms.Button bt_addall;
private System.Windows.Forms.Button bt_delall;
private System.Windows.Forms.Button bt_delpart;
private System.Windows.Forms.ListBox lb_result;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Button bt_clearperson;
private System.Windows.Forms.Button bt_addperson;
private System.Windows.Forms.RichTextBox rtb_text;
后台代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//添加学院
cb_collage.Items.Add("机械与车辆工程学院");
cb_collage.Items.Add("信息科学与工程学院");
for (int i = 2010; i < 2021; i++)
{
//添加年级
cb_grade.Items.Add(i + "");
//添加出生日期的年份
cb_year.Items.Add(i - 20 + "");
}
for (int i = 1; i < 13; i++)
{
//添加出生日期的月份
cb_mouth.Items.Add(i + "");
}
//添加课程
lb_base.Items.Add("计算机科学导论");
lb_base.Items.Add("高等数学");
lb_base.Items.Add("数据结构");
lb_base.Items.Add("编译原理");
lb_base.Items.Add("微机原理");
lb_base.Items.Add("数据库原理");
lb_base.Items.Add(".NET程序设计");
lb_base.Items.Add("Java程序设计");
}
private void cb_collage_SelectedValueChanged(object sender, EventArgs e)
{
cb_zhuanye.Items.Clear();
string collage = cb_collage.SelectedItem.ToString();
switch (collage)
{
case "机械与车辆工程学院":
cb_zhuanye.Items.Add("机械专业");
cb_zhuanye.Items.Add("车辆专业");
break;
case "信息科学与工程学院":
cb_zhuanye.Items.Add("计科");
cb_zhuanye.Items.Add("软件");
break;
}
cb_zhuanye.SelectedIndex = 0;
}
private void cb_zhuanye_SelectedValueChanged(object sender, EventArgs e)
{
cb_class.Items.Clear();
string zhuanye = cb_zhuanye.SelectedItem.ToString();
switch (zhuanye)
{
case "机械专业":
cb_class.Items.Add("机械1班");
cb_class.Items.Add("机械2班");
break;
case "车辆专业":
cb_class.Items.Add("车辆1班");
cb_class.Items.Add("车辆2班");
break;
case "计科":
cb_class.Items.Add("计科1班");
cb_class.Items.Add("计科2班");
cb_class.Items.Add("计科3班");
break;
case "软件":
cb_class.Items.Add("软件1班");
cb_class.Items.Add("软件2班");
break;
}
cb_class.SelectedIndex = 0;
}
private void cb_year_SelectedIndexChanged(object sender, EventArgs e)
{
//如果选了月份之后 又选择或修改了年份
if (cb_mouth.SelectedItem != null)
{
getDays();
}
}
private void cb_mouth_SelectedIndexChanged(object sender, EventArgs e)
{
//选了年份之后 又选择或修改了月份
if (cb_year.SelectedItem != null)
{
getDays();
}
}
//根据年份和月份确定日期
public void getDays()
{
cb_day.Items.Clear();
int mouth = int.Parse(cb_mouth.SelectedItem.ToString());
int year = int.Parse(cb_year.SelectedItem.ToString());
int index = 0;
if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12)
{
index = 31;
}
else if (mouth == 2)
{
//判断是否闰年
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
index = 29;
}
else
{
index = 28;
}
}
else
{
index = 30;
}
for (int i = 1; i < (index + 1); i++)
{
cb_day.Items.Add(i + "");
}
}
private void bt_addpart_Click(object sender, EventArgs e)
{
if (lb_base.SelectedItem!=null)
{
string result = lb_base.SelectedItem.ToString();
lb_result.Items.Add(result);
lb_base.Items.Remove(result);
}
}
private void bt_addall_Click(object sender, EventArgs e)
{
lb_result.Items.AddRange(lb_base.Items);
lb_base.Items.Clear();
}
private void bt_delall_Click(object sender, EventArgs e)
{
lb_base.Items.AddRange(lb_result.Items);
lb_result.Items.Clear();
}
private void bt_delpart_Click(object sender, EventArgs e)
{
if (lb_result.SelectedItem!=null)
{
string result = lb_result.SelectedItem.ToString();
lb_base.Items.Add(result);
lb_result.Items.Remove(result);
}
}
private void bt_addperson_Click(object sender, EventArgs e)
{
string stu_no = tb_stuno.Text;
string name = tb_name.Text;
string college = cb_collage.SelectedItem != null?cb_collage.SelectedItem.ToString():"";
string zhuanye = cb_zhuanye.SelectedItem != null?cb_zhuanye.SelectedItem.ToString():"";
string grade = cb_grade.SelectedItem!=null?cb_grade.SelectedItem.ToString():"";
string class_ = cb_class.SelectedItem != null ? cb_class.SelectedItem.ToString() : "";
string sex = "";
//添加信息
if (rb_man.Checked) {
sex = "男";
}
if (rb_women.Checked)
{
sex = "女";
}
string brith = "";
if (cb_year.SelectedItem != null && cb_mouth.SelectedItem != null && cb_day.SelectedItem!=null)
{
brith = cb_year.SelectedItem.ToString() + "--" + cb_mouth.SelectedItem.ToString() + "--" + cb_day.SelectedItem.ToString();
}
string habits = "";
//获取爱好
foreach(Control con in gb_habit.Controls){
if(con is CheckBox){
CheckBox cb = (CheckBox)con;
if(cb.Checked){
habits = habits + cb.Text + " ";
}
}
}
//获取课程
string course = "";
for (int i = 0; i < lb_result.Items.Count;i++ )
{
if (i != (lb_result.Items.Count - 1))
{
course = course + lb_result.Items[i] + ",";
}
else {
course = course + lb_result.Items[i];
}
}
string temp = rtb_text.Text;
string result_msg = string.Format("学号:{0},姓名:{1},学院:{2},专业:{3},年级:{4},班级:{5},性别:{6},出生日期:{7},业余爱好:{8},课程:{9},备注:{10}",stu_no,name,college,zhuanye,grade,class_,sex,brith,habits,course,temp);
MessageBox.Show(result_msg);
}
}