一、实验名称:
二、实验目的:
1、熟悉Microsoft Visual C# 2008 Windows窗体应用程序设计具体应用
三、实验器材:
1、计算机
四、实验内容:
1、 主菜单设计
1)新建一个名为MainMenu的窗体应用程序,将窗体 Text属性设为“主菜单演示”
2)工具箱->公共控件->TextBox,拖动其进入窗体,在窗体中添加一个文本框textBox1
3)工具箱->菜单和工具栏->MenuStrip,拖动其进入窗体,如下图:
4)建立如下所示菜单:
在输入菜单选项名称时,用符号“&”引导一个英文字母表示可用快捷键Alt加该英文字母调出该选项。
5)双击各菜单项,给各菜单项添加如下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void 重置RToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "欢迎使用主菜单";
}
private void 清空DToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("主菜单演示,菜单选项的使用", "提示");
}
}
}
运行程序,测试菜单功能,如下图:
2、 上下文菜单设计
1)新建一个名为MainMenu的窗体应用程序,将窗体 Text属性设为“上下文菜单”
2)工具箱->公共控件->TextBox,拖动其进入窗体,在窗体中添加一个文本框textBox1
3)工具箱->菜单和工具栏->ContextMenuStrip,拖动其进入窗体,添加一个上下文菜单控件,如下图:
5)建立如下上下文菜单:
注意:在“文件”和“帮助”两个菜单项间添加了一条分隔线,方法是输入破折号“-”。
6)双击各菜单项,添加代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 清空DToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void 重置RToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "上下文菜单演示";
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("上下文菜单演示", "提示");
}
}
}
7)在TextBox属性窗口中,在ContextMenuStrip属性中打开下拉菜单,选择确定为contextMenuStrip1
8)运行程序。此时,在文本框中右击就可弹出上下文菜单。
3、 单选框和复选框
1)新建一个名为E29的窗体应用程序,属性Text改为“单选框和复选框”
2)添加三个分组框,将Text属性分别改名为“性别”、“兴趣”和“用户兴趣”,步骤如下:
工具箱->容器->GroupBox,拖动至窗体,即可添加
3)在分组框“性别”中添加两个单选按钮RadioButton控件,步骤如下:
工具箱->公共控件->RadioButton,将Text属性分别改为“男”和“女”
4)在分组框“兴趣”中添加四个复选框CheckBox控件,步骤如下:
工具箱->公共控件->CheckBox,将Text属性分别改为“音乐”、“电影”、“游戏”和“旅行”
5)在分组框“用户兴趣”中添加两个标签Label控件,步骤如下:
工具箱->公共控件->Label,将Text属性分别改为“性别未知”和“兴趣未知”,如下图:
6)分别双击“男”和“女”两个单选按钮,分别输入如下代码:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "男";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "女";
}
7)分别双击各复选框,输入如下代码(四个复选框中代码相同,以第一个为例):
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string temp = "";
if (checkBox1.Checked == true)
temp = temp + checkBox1.Text;
if (checkBox2.Checked == true)
temp = temp + checkBox2.Text;
if (checkBox3.Checked == true)
temp = temp + checkBox3.Text;
if (checkBox4.Checked == true)
temp = temp + checkBox4.Text;
if (temp == "")
label2.Text = "用户没有选择任何兴趣";
else
label2.Text = temp;
}
8)保存后运行程序,如下图:
4、 列表框和组合框
1)新建一个名为E30的窗体,Text改为“列表框和组合框”
2)在窗体中添加七个控件,添加完成后的效果如下图:
各控件要求如下表:
序号 | 类型 | 属性 | 值 |
1 | ListBox | Name | listBoxYueShouRu |
2 | ComboBox | Name | comboBoxZhengJianLeiXing |
3 | GroupBox | Name Text | groupBoxUser 用户信息 |
4 | Label | Name Text | labelYueShouRu 月收入 |
5 | Label | Name Text | labelShouRu 收入 |
6 | Label | Name Text | labelZhengJianLeiXing 证件类型 |
7 | Label | Name Text | labelZhengJian 证件 |
3)双击窗体空白处进入代码编辑窗口,输入如下代码:
private void Form1_Load(object sender, EventArgs e)
{
//ListBox初始化
listBoxYueShouRu.Items.Add("100以下");
listBoxYueShouRu.Items.Add("1000-2000");
listBoxYueShouRu.Items.Add("2001-3000");
listBoxYueShouRu.Items.Add("3000-4000");
listBoxYueShouRu.Items.Add("4000-5000");
listBoxYueShouRu.Items.Add("5000-6000");
listBoxYueShouRu.Items.Add("6000-7000");
listBoxYueShouRu.Items.Add("6000-7000");
listBoxYueShouRu.Items.Add("7000-8000");
listBoxYueShouRu.Items.Add("8000-9000");
listBoxYueShouRu.Items.Add("9000-10000");
listBoxYueShouRu.Items.Add("10000以上");
//ComboBox初始化
comboBoxZhengJianLeiXing.Items.Add("身份证");
comboBoxZhengJianLeiXing.Items.Add("学生证");
comboBoxZhengJianLeiXing.Items.Add("教师证");
comboBoxZhengJianLeiXing.Items.Add("军人证");
comboBoxZhengJianLeiXing.Items.Add("护照");
}
4)分别双击ListBox和ComboBox,分别输入如下代码:
private void listBoxYueShouRu_SelectedIndexChanged(object sender, EventArgs e)
{
labelShouRu.Text = listBoxYueShouRu.SelectedItem.ToString();
}
private void comboBoxZhengJianLeiXing_SelectedIndexChanged(object sender, EventArgs e)
{
labelZhengJian.Text = comboBoxZhengJianLeiXing.SelectedItem.ToString();
}
5)运行效果如下图:
1)新建一个名为E31的窗体,Text改为“列表视图”
2)在窗体中中添加五个控件,添加完成后的效果如下图:
各控件要求如下表:
序号 | 类型 | 属性 | 值 |
1 | ImageList | Name Images | imageListanimal 添加六张动物图片 |
2 | ListView | Name SmallImageList View | listViewanimal imageListanimal SmallIcon |
3 | Label | Name Text | labelTishi1 喜欢的动物 |
4 | Label | Name Text | labelTishi2 你最喜欢的动物是: |
5 | Label | Name Text | labelAnimal 未知 |
3)双击窗体空白处进入代码编辑窗口,输入如下代码:
private void Form1_Load(object sender, EventArgs e)
{
listViewanimal.Items.Add("大象",0);
listViewanimal.Items.Add("狗", 1);
listViewanimal.Items.Add("猫",2);
listViewanimal.Items.Add("青蛙",3);
listViewanimal.Items.Add("蛇",4);
listViewanimal.Items.Add("兔子",5);
listViewanimal.Items.Add("乌龟",6);
}
4)双击ListView控件,输入如下代码:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
labelAnimal.Text = listViewanimal.FocusedItem.Text;
}
5)运行效果如下图:
6、 树视图
1)新建一个名为E32的窗体,Text改为“树视图”,Size设为“400,500”
2)添加一个TreeView控件,将Size属性改为“390,470”,并将Anchor属性改为“Top, Bottom, Left, Right”
3)添加一个ImageList控件,为其添加“我的电脑”,“磁盘驱动器”,“文件夹”,“打开的文件夹”四个图标(通过Images属性实现)
4)将TreeView的ImageList属性置为imageList1,使两者相关联
5)打开TreeView属性Nodes的树节点编辑器,添加一个根,将标签改为“我的电脑”,在“ImageKey”和“SelectedImageKey”中选择相应图标。
6)完成后的效果如下图:
7)双击TreeView,输入如下图标:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Text.ToString() != "我的电脑")
EnumDirectories(e.Node);
else
EnumDrives(e.Node);
}
以上的AfterSelect事件中用到EnumDirectories和EnumDrives两个方法,其中还用到了System.IO下的Directory,需在头部添加“using System.IO;”。两个方法的实现代码如下:
private void EnumDrives(TreeNode ParentNode)
{
if(ParentNode.Nodes.Count==0)
{
foreach(string drive in Directory.GetLogicalDrives())
{
treeView1.SelectedNode=ParentNode;
TreeNode TempNode=new TreeNode();
TempNode.Text=drive.Substring(0,drive.Length-1);
TempNode.Tag=drive;
TempNode.ImageIndex=1;
TempNode.SelectedImageIndex = 1;
treeView1.SelectedNode.Nodes.Add(TempNode);
treeView1.SelectedNode.Nodes[treeView1.SelectedNode.Nodes.Count - 1].EnsureVisible();
}
}
}
private void EnumDirectories(TreeNode ParentNode)
{
treeView1.SelectedNode = ParentNode;
string DirectoryPath = ParentNode.Tag.ToString();
if (ParentNode.Nodes.Count == 0)
{
if (DirectoryPath.Substring(DirectoryPath.Length - 1) != @"\")
DirectoryPath += @"\";
try
{
foreach (string directory in Directory.GetDirectories(DirectoryPath))
{
TreeNode TempNode = new TreeNode();
TempNode.Text = directory.Substring(directory.LastIndexOf(@"\") + 1);
TempNode.Tag = directory;
TempNode.ImageIndex = 3;
TempNode.SelectedImageIndex = 2;
treeView1.SelectedNode.Nodes.Add(TempNode);
treeView1.SelectedNode.Nodes[treeView1.SelectedNode.Nodes.Count - 1].EnsureVisible();
}
}
catch (Exception)
{
}
}
}
8)运行效果如下图:
7、 Timer控件
1)新建一个名为E32的窗体,Text改为“Timer控件”
2)添加一个Timer控件,设置其Interval属性为10,表示每隔10毫秒发生一个Tick事件
3)添加两个Button控件,Text属性分别改为:“开始/继续”(表示开始计时或停止后继续开始计时)和“停止/清零”(表示停止计时或在计时停止时将计时器清零),
4)添加一GroupBox控件,Text改为“计时器”
5)在GroupBox中添加三个Label控件用于显示分钟数、秒数及精确到0.01毫秒的小数,Name分别设为labelFen,labelMiao,labelHaomiao,Text都设为0;再添加两个Label控件,Text都设为“:”,用于分隔前三个控件
6)双击“开始/继续”按钮,添加如下代码:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
7)双击“停止/清零”按钮,添加如下代码:
private void button2_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
timer1.Enabled = false;
else
{
labelFen.Text = "0";
labelMiao.Text = "0";
labelHaomiao.Text = "0";
}
}
8)双击Timer控件,添加如下代码:
private void timer1_Tick(object sender, EventArgs e)
{
int min = Int32.Parse(labelFen.Text);
int sec = Int32.Parse(labelMiao.Text);
int fra = Int32.Parse(labelHaomiao.Text);
fra++;
//分钟数
if (sec == 60)
{
min++;
labelFen.Text = min.ToString();
labelMiao.Text = "0";
if (min == 100)
{
timer1.Enabled = false;
MessageBox.Show("计时器已达到上限", "提示");
return;
}
}
//秒数
if (fra == 100)
{
sec++;
labelMiao.Text = sec.ToString();
}
//秒数小数位
fra = fra % 100;
labelHaomiao.Text = fra.ToString();
}
9)运行效果如下图:
8、 时钟控件和日历控件
1)新建一个名为E35的窗体,Text改为“日历”,Name改为“FormCalendar”,Size改为“300,320”
2)添加以下几个控件并进行相应设置:
序号 | 类型 | 属性 | 值 | 备注 |
1 | DateTimePicker | Name ShowUpDown Font | dateTimePickerCalendar True 宋体,15.75pt,style=Bold | ShowUpDown设为True防止单击下拉按钮时显示MonthCalendar控件 |
2 | MonthCalendar | Name | monthCalendarCalendar |
|
3 | TrackBar | Name | trackBarCalendar |
|
4 | Label | Name Text | labelLowSpeed 低速 |
|
5 | Label | Name Text | labelHighSpeed 高速 |
|
6 | Button | Name Text Enabled ForeColor | buttonRun 运行 False GrayText |
|
7 | Button | Name Text | buttonStop 停止 |
|
8 | Button | Name Text | buttonReset 重置 |
|
9 | Timer | Name Enabled InterVal | timerCalendar True 1000 |
|
效果如下:
3)双击各相应控件,添加对应代码,完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E35
{
public partial class FormCalendar : Form
{
public FormCalendar()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
dateTimePickerCalendar.Value = dateTimePickerCalendar.Value.AddSeconds(trackBarCalendar.Value * 60 + 1);
monthCalendarCalendar.TodayDate = dateTimePickerCalendar.Value;
}
private void FormCalendar_Load(object sender, EventArgs e)
{
dateTimePickerCalendar.Value = DateTime.Now;
monthCalendarCalendar.TodayDate = DateTime.Now;
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
if (trackBarCalendar.Value > 0)
timerCalendar.Interval = 10;
else
timerCalendar.Interval = 1000;
}
private void button1_Click(object sender, EventArgs e)
{
buttonStop.ForeColor = buttonReset.ForeColor;
buttonStop.Enabled = true;
timerCalendar.Enabled = true;
buttonRun.ForeColor = Color.Gray;
buttonRun.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
buttonRun.ForeColor = buttonReset.ForeColor;
buttonRun.Enabled = true;
timerCalendar.Enabled = false;
buttonStop.ForeColor = Color.Gray;
buttonStop.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
trackBarCalendar.Value = 0;
dateTimePickerCalendar.ResetText();
}
}
}
4)运行效果如下图:
9、 MDI(多文档)窗口
1)新建一个名为E36的窗体,Text改为“多文档窗口”,Name改为“FormMain”,WindowState改为“Maximized”
2)添加一个MenuStrip控件,Name改为“menuStripMainMenu”,建立如下菜单结构:
菜单━━━文件━━━新建
┃ ┗━━关闭
┗━排列━━━层叠
┃━━垂直
┗━━水平
3)双击各菜单项,添加对应代码,完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E36
{
public partial class FormMain : Form
{
private static int FormCount = 0;//设置一个私有整形变量,记录新建窗口编号
public FormMain()
{
InitializeComponent();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
Form temp = new Form();
temp.MdiParent = this;
temp.Text = "窗口#" + FormCount.ToString();
FormCount++;
temp.Show();
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 水平HToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void 垂直VToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void 水平HToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileVertical);
}
}
}
//说明:利用系统提供的LayoutMdi方法来排列窗口
4)运行效果如下图:
五、实验要求:
1、填写完整的实验报告。
2、总结Microsoft Visual C# 2008 Windows窗体应用程序设计的一般步骤。
3、记录上机过程中所出现的相关英文信息如菜单项名称、错误提示等,查出其中文含义,并写在实验报告后面。
4、查看资料,熟悉总结Microsoft Visual C# 2008 Windows窗体应用程序设计细节。