资源描述
实验报告
学院(系)名称:计算机科学与工程学院
姓名
**
学号
********
专业
计算机科学与技术
班级
2015级 班
实验项目
实验二:Windows 窗体程序开发
课程名称
. NET程序设计
课程代码
0667066
实验时间
2017年4月25日
实验地点
7-215
批改意见
成绩
教师签字:
一、 实验目的
(1) 掌握如何创建 Windows 窗体应用程序并熟悉程序的结构;
(2) 掌握常用控件的使用;掌握控件的常用属性;
(3) 理解事件的含义,并掌握控件的常用事件的含义及应用;
(4) 掌握不同窗体之间传递数据的方式;
(5) COM 组件的应用。
二、 实验环境
n 开发环境:PC机,Windows 7操作系统;
n 开发工具:Visual Studio 2010以上。
三、 实验要求
(1) 认真完成实验内容,编写程序代码;
(2) 输出实验的正确结果;
(3) 书写并提交实验报告。
四、 实验内容
1、 文本框控件使用
利用文本框控件的属性及事件,按如下图窗体所示完成:当输入单价和数量时能够自动正确计算出商品价值。
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
double x = 0;
int y = 0;
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
try
{
x = Convert.ToDouble(textBox1.Text);
}
catch
{
MessageBox.Show("请输入数字");
}
if (textBox2.Text != "")
{
textBox3.Text = (x * y).ToString();
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
try
{
y = Convert.ToInt32(textBox2.Text);
}
catch
{
MessageBox.Show("请输入数字");
}
textBox3.Text = (x * y).ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
2、 Timer 组件的使用,要求利用 Timer 组件实现一时钟。
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
}
}
运行结果:
3、 ComboBox 控件的使用
已知数组 data 中存放政治面貌的值,试将 data 中的值初始化到 ComboBox控件中,并实现当选中某个值时将其显示在一个 TextBox 控件中。
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2._3
{
public partial class Form1 : Form
{
string[] data = {"中共党员","共青团员","群众","民主党派","无党人士","其他"};
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < data.Length; i++)
{
comboBox1.Items.Add(data[i]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.Text;
}
}
}
运行结果:
4、 ListView 控件的使用
将二维数组中的信息在 ListView 控件中。二维数组用来模拟学生信息,具体信息: string[,] data = { { "20140001", "张三", "天津市西青区宾水西道391号" }, { "20140002", "李四", "天津市西青区宾水西道391号" } }; 试完成如下功能:
(1) 将 data 信息显示在 ListView 控件中;
(2) 当点击 ListView 控件中某个学生信息时,将相应的信息显示在对应的文本框控件中。
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2._4
{
public partial class Form1 : Form
{
string[] data1 = { "1","20140001", "张三", "天津市西青区宾水西道391号" };
string[] data2 = {"2", "20140002", "李四", "天津市西青区宾水西道391号" };
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Columns.Add("序号",50,HorizontalAlignment.Center);
listView1.Columns.Add("学号", 80, HorizontalAlignment.Center);
listView1.Columns.Add("姓名", 80, HorizontalAlignment.Center);
listView1.Columns.Add("地址",290, HorizontalAlignment.Center);
ListViewItem lvi = new ListViewItem(data1);
listView1.Items.Add(lvi);
lvi = new ListViewItem(data2);
listView1.Items.Add(lvi);
}
private void listView1_Click(object sender, EventArgs e)
{
textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text;
textBox2.Text = listView1.SelectedItems[0].SubItems[2].Text;
textBox3.Text = listView1.SelectedItems[0].SubItems[3].Text;
}
}
}
运行结果:
5、 控件综合应用
(1)创建一个登录窗体界面,当用户输入:admin/admin 时登录成功进入主界面。当输入的用户名和密码不等于 admin/admin 时应弹出提示对话框。
(2)主界面设计与实现,要求:
1.主界面设置为 MDI 窗体;
2.主界面应具有菜单、工具栏及状态栏;
3.将登录窗口输入的用户名显示在状态栏中;
4.主界面中提供退出应用程序的功能;
5.点击某个菜单或工具栏时,能够显示出对应的窗体,并作为主窗体的 MDI 子窗体。
代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2._5
{
public partial class MainForm : Form
{
public string username;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = username;
toolStripLabel1.Text = "用户管理";
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
}
private void toolStripLabel1_Click_1(object sender, EventArgs e)
{
}
private void 退ª?出?ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.MdiParent = this.Owner;
MessageBox.Show(zi.IsMdiChild.ToString());
zi.Show();
}
private void 系¦Ì统ª3管¨¹理¤¨ªSToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.MdiParent = this.Owner;
zi.Show();
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.Show();
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.MdiParent = this.Owner;
zi.Show();
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.MdiParent = this.Owner;
zi.Show();
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
ziForm1 zi = new ziForm1();
zi.MdiParent = this.Owner;
zi.Show();
}
}
}
运行结果:
6、COM 组件的应用
利用微软提供的 Windows Media Player 组件实现音乐播放器功能。建议:结合
ListBox 控件实现。功能及界面自己设计,功能尽量全,界面美观,布局合理
代码实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _2._6
{
public partial class Form1 : Form
{
int index1 = 0;
int i = 0;
double currentPosition = 0;
double fileLength = 0;
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.URL = "C:\\Users\\57278\\Desktop\\2.6\\source\\" + 0.ToString() + ".mp3";
}
string[] musiclist = { "火星人来过.mp3", "薛之谦-初学者.mp3", "薛之谦-丑八怪.mp3", "薛之谦-你还要我怎样,mp3", "刚刚好.mp3", "演员.mp3" };
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
index1 = listBox1.SelectedIndex;
axWindowsMediaPlayer1.URL = "C:\\Users\\57278\\Desktop\\2.6\\source\\" + listBox1.SelectedIndex.ToString() + ".mp3";
}
private void Form1_Load(object sender, EventArgs e)
{
for (i = 0; i < musiclist.Length; i++)
{
listBox1.Items.Add(musiclist[i]);
}
}
//private void axWindowsMediaPlayer1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
//{
// int index = listBox1.SelectedIndex;
// index--;
// axWindowsMediaPlayer1.URL = "C:\\Users\\Administrator\\Desktop\\2.6\\source\\" + index.ToString() + ".mp3";
//}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
}
private void axWindowsMediaPlayer1_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
currentPosition = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
fileLength = axWindowsMediaPlayer1.currentMedia.duration;
// string title = axWindowsMediaPlayer1.currentMedia.getItemInfo("title");
// MessageBox.Show(title);
if (fileLength - currentPosition <= 1.0)
{
index1++;
if (index1 >= musiclist.Length)
{
index1 -= musiclist.Length;
}
listBox1.SelectedIndex = index1;
axWindowsMediaPlayer1.URL = "C:\\Users\\57278\\Desktop\\2.6\\source\\" + index1.ToString() + ".mp3";
}
}
}
}
运行结果
五、 心得体会
这次试验让我明白了两年大学没白上,如果是在高中这么几个程序肯定写不出来,并不是说当时没学过,而是一种能力。书上只给了能用到的一些控件的使用方法,并没有给出特定的实现方法,相当于我们有了工具,知道了它的一部分使用方法现在来使用它去完成一项全新的任务。经过两年的大学生活我懂了运用自己已经掌握的知识来尝试,上网各种搜索,问老师,问同学。确实是能力一步一步的提高了。
【本文档内容可以自由复制内容或自由编辑修改内容期待你的好评和关注,我们将会做得更好】
精选范本,供参考!
展开阅读全文