实验三
实验目的
1. 建立掌握Windows应用程序的步骤和方法。
2. 掌握窗体、菜单和对话框的使用。
3. 掌握控件及其使用方法。
实验内容
①实验4-1:
跟着学习
创建窗体与菜单练习。
1.创建窗体与菜单。
新建一个Windows应用程序,在工具箱里拖拽MenuStrip菜单组件,添加到当前窗口,即可进行菜单编辑。如图所示:
以简单的“退出”为例编写代码。双击“退出”按钮,添加代码如下:
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.Close();
}
2.按F5键进行调试。
因为只有“退出”可以产生响应事件,故单击“退出”按钮,关闭当前程序。
自己思考
以编程的方式实现上述菜单结构。
源代码:
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 shiyan4_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MenuStrip menu = new MenuStrip();
ToolStripMenuItem iteml = new ToolStripMenuItem("文件");
ToolStripMenuItem item2 = new ToolStripMenuItem("编辑");
ToolStripMenuItem item3 = new ToolStripMenuItem("帮助");
menu.Items.AddRange(new ToolStripItem[] { iteml, item2, item3 });
ToolStripMenuItem item4 = new ToolStripMenuItem("打开");
ToolStripMenuItem item5 = new ToolStripMenuItem("保存");
ToolStripMenuItem item6 = new ToolStripMenuItem("退出");
iteml.DropDownItems.AddRange(new ToolStripItem[] { item4, item5, item6 });
this.Controls.Add(menu);
}
}
}
自己新建窗体。在“解决方案资源管理器”中右键单击项目名称,选择“添加”→“Windows 窗体”,在弹出的“添加新项”对话框中选择需要的模板即可,这里选择“Windows窗体”,重命名为MyForm.cs,单击“添加”按钮,一个新的窗体创建完成。
添加Windows窗体:
重新命名并添加:
②实验4-2
跟着学习:
命令按钮、单选按钮和复选框等窗体控件练习。
设置界面
在新建的窗体MyForm中添加命令按钮、单选按钮和复选框等控件,如图所示:
添加3个单选按钮 RadioButton,放在1个GroupBox控件中,用来设置程序的背景颜色。6个复选框ChechBox,同样放在另一个GroupBox控件中,用来选择喜欢的颜色。1个TextBox控件,用来显示喜欢颜色的文字。1个Button按钮用来退出程序。
添加代码。
代码如下:
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 shiyan4_1
{
public partial class MyForm1 : Form
{
public MyForm1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
this.BackColor = Color.Red;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
this.BackColor = Color.Green;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
this.BackColor = Color.Blue;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox1.Text + "、";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox2.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox2.Text + "、";
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox3.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox3.Text + "、";
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox4.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox4.Text + "、";
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox5.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox5.Text + "、";
}
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox6.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox6.Text + "、";
}
}
}
运行程序
为主窗体的“打开”菜单中添加一个事件过程,用于启动MyForm窗口:
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
MyForm1 myForm = new MyForm1();
myForm.Show();
}
按F5键编译运行,单击“文件”→“打开”按钮,弹出MyForm窗口,在其中可以设置窗口背景色和选择自己喜欢的颜色,如图所示:
自己思考
删除两个GroupBox控件,要求保持原来的功能,修改程序,编译运行,观察运行结果。
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 shiyan4_1
{
public partial class MyForm1 : Form
{
public MyForm1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
this.BackColor = Color.Red;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
this.BackColor = Color.Green;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
this.BackColor = Color.Blue;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox1.Text + "、";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox2.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox2.Text + "、";
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox3.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox3.Text + "、";
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox4.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox4.Text + "、";
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox5.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox5.Text + "、";
}
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox6.Checked == true)
this.YourColor.Text = YourColor.Text + checkBox6.Text + "、";
}
private void YourColor_TextChanged(object sender, EventArgs e)
{
}
}
}
2.添加一个“确定”按钮,要求按下该按钮后遍历checkbox控件,将选中的颜色显示在文本框内。
源代码:
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 shiyan4_1
{
public partial class MyForm1 : Form
{
public MyForm1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
this.BackColor = Color.Red;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
this.BackColor = Color.Green;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
this.BackColor = Color.Blue;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
}
private void YourColor_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox1.Text + "、"; }
if (this.checkBox2.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox2.Text + "、"; }
if (this.checkBox3.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox3.Text + "、"; }
if (this.checkBox4.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox4.Text + "、"; }
if (this.checkBox6.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox6.Text + "、"; }
if (this.checkBox5.Checked == true)
{ this.YourColor.Text = YourColor.Text + checkBox5.Text + "、"; }
}
}
}
运行结果:
③实验4-3:
A.跟着学习
标签控制、文本框控制、列表框控制和组合框控制等窗体控件练习。
1.设置窗体。
新建项目,在窗体上添加两个标签label1、labe12,再添加1个文本框textBox_Bookname,1个组合框comboBox_Publishing,并用1个GroupBox控件组合起来。添加2个按扭Button_Add和Button_Remove,用1个GroupBox控件组合起来,用来添加和移出数据项。最后添加1个listBox_Book控件,如图所示。
2.添加代码。
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 shiyan4_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("书名:" + textBox1.Text + ",出版社:" + comboBox1.Text);
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if(listBox1.SelectedIndex!=-1)
{
listBox1.Items.Remove(this.listBox1.SelectedItem);
}
}
}
}
按F5键编译运行,用户操作界面如图所示。
B.自己思考
在描述书的数据项中添加“单价”、“是否有光盘”等项目,重新完善上述程序。编译运行,观察运行结果。
源代码:
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 shiyan4_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("书名:" + textBox1.Text + ",出版社:" + comboBox1.Text+",单价:"+textBox2.Text+",是否有光盘:"+ comboBox2.Text );
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if(listBox1.SelectedIndex!=-1)
{
listBox1.Items.Remove(this.listBox1.SelectedItem);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
实验结果:
实验总结
通过本次实验更加熟悉了Windows应用程序的建立,掌握窗体、对话框和菜单的使用以及控件的使用。将课本知识与实践相结合,及时发现自身的不足之处。