Windows窗体程序的开发实验报告.docx
《Windows窗体程序的开发实验报告.docx》由会员分享,可在线阅读,更多相关《Windows窗体程序的开发实验报告.docx(18页珍藏版)》请在冰点文库上搜索。
Windows窗体程序的开发实验报告
精通C#与.NET4.0数据库开发
试验汇报
试验题目:
Windows窗体程序开发
专业计算机科学与技术
学生姓名
班级学号
教师
指导单位
日期
老师评语
老师署名:
年月日
成绩评定
备注
一、试验目
1.学会在VisualStudio中创建和运行窗体程序。
2.掌握Windows窗体基础操作。
3.学会使用常见Windows控件。
4.学会使用菜单和工具栏以及通用对话框。
二、试验环境
.NET框架开发环境VisualStudio
三、试验内容
例5-1、2:
源代码:
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidAddInputToLog(stringinput)
{
this.tbLog.AppendText("\r\n"+input);
this.tbLog.ScrollToCaret();
}
privatevoidbtnSubmit_Click(objectsender,EventArgse)
{
stringinput=this.tbInput.Text;
this.AddInputToLog(input);
this.tbInput.Clear();
}
}
例5-3、4:
源代码:
publicpartialclassFrmMain:
Form
{
publicFrmMain()
{
InitializeComponent();
}
privatevoidbtnSetProp_Click(objectsender,EventArgse)
{
this.Text="测试对话框";
this.FormBorderStyle=FormBorderStyle.FixedDialog;
this.BackColor=Color.Gray;
this.WindowState=FormWindowState.Normal;
this.MinimizeBox=false;
this.Height=200;
this.Width=400;
this.TopMost=true;
}
privateFrmMain_CurrFrm=null;
privatevoidbtnCreate_Click(objectsender,EventArgse)
{
if(this._CurrFrm==null)
{
this._CurrFrm=newFrmMain();
this._CurrFrm.Show();
}
else
{
this._CurrFrm.Activate();
}
}
privatevoidbtnClose_Click(objectsender,EventArgse)
{
if(this._CurrFrm!
=null)
{
this._CurrFrm.Close();
this._CurrFrm=null;
}
}
privatevoidFrmMain_Load(objectsender,EventArgse)
{
}
}
例5-5:
源代码:
//label1参数设置
this.label1.AutoSize=true;
this.label1.BackColor=System.Drawing.Color.Red;
this.label1.Font=newSystem.Drawing.Font("楷体",
12F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,
((byte)(134)));
this.label1.ForeColor=System.Drawing.Color.White;
this.label1.Location=newSystem.Drawing.Point(68,43);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(232,16);
this.label1.TabIndex=0;
this.label1.Text="红底白字,楷体小四号,无边框";
this.label1.Click+=newSystem.EventHandler(this.label1_Click);
//label2参数设置
this.label2.AutoSize=true;
this.label2.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Font=newSystem.Drawing.Font("幼圆",
15F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(134)));
this.label2.Location=newSystem.Drawing.Point(49,79);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(289,22);
this.label2.TabIndex=1;
this.label2.Text="幼圆小三粗体,Single边框";
this.label2.Click+=newSystem.EventHandler(this.label2_Click);
例5-6:
源代码:
publicpartialclassForm1:
Form
{
privateint_Value=0;
//btnShowMsgClick事件处理函数
privatevoidbtnShowMsg_Click(objectsender,EventArgse)
{
MessageBox.Show(string.Format("现在需要:
{0}",this._Value));
}
//“多一点”按钮Click事件处理函数
privatevoidbtnAdd_Click(objectsender,EventArgse)
{
this._Value++;
this.lbRes.Text=string.Format("现在需要:
{0}",this._Value);
}
//“少一点”按钮Click事件处理函数
privatevoidbtnSub_Click(objectsender,EventArgse)
{
this._Value--;
this.lbRes.Text=string.Format("现在需要:
{0}",this._Value);
}
}
例5-7:
源代码:
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbuttons_CheckedChanged(objectsender,EventArgse)
{
stringweeks="";
weeks+=ckbWeek1.Checked?
"星期一":
"";
weeks+=ckbWeek2.Checked?
"星期二":
"";
weeks+=ckbWeek3.Checked?
"星期三":
"";
weeks+=ckbWeek4.Checked?
"星期四":
"";
weeks+=ckbWeek5.Checked?
"星期五":
"";
weeks+=ckbWeek6.Checked?
"星期六":
"";
weeks+=ckbWeek7.Checked?
"星期日":
"";
stringsport="";
sport+=rbSword.Checked?
"击剑":
"";
sport+=rbJump.Checked?
"跳水":
"";
sport+=rbTiCao.Checked?
"体操":
"";
stringjiangpai="";
jiangpai+=rbGold.Checked?
"金牌":
"";
jiangpai+=rbSliver.Checked?
"银牌":
"";
jiangpai+=rbTong.Checked?
"铜牌":
"";
this.lbHint.Text=weeks+sport+jiangpai;
}
}
例5-8:
源代码:
publicpartialclassForm1:
Form
{
privatevoidForm1_Load(objectsender,EventArgse)
{
this.mtbMobile.Mask="";//手机号码:
13后面9个必填数字
this.mtbPhone.Mask="";//电话:
4位必填区号,7或8位号码?
this.tbName.Text="";
this.tbUsers.Text="";
}
privatevoidbtnAdd_Click(objectsender,EventArgse)
{
stringusr=string.Format("<{0}>:
<{1}>:
<{2}>",//产生用户信息
this.tbName.Text,
this.mtbPhone.Text,
this.mtbMobile.Text);
//添加到用户统计文本框¨°
this.tbUsers.AppendText(usr+System.Environment.NewLine);
this.mtbMobile.Text="";//清空用户信息
this.mtbPhone.Text="";
this.tbName.Text="";
}
}
例5-9:
源代码:
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
//设置cmbHouXuan只能从ComboBox中已经有候选值选择
this.cmbHouXuan.DropDownStyle=ComboBoxStyle.DropDownList;
//lstResult只能实施单选,而且对全部值进行排序
this.lstResults.SelectionMode=SelectionMode.One;
this.lstResults.Sorted=true;
this.GenerateCombItems();//产生ComboBox中D可选项
}
privatevoidGenerateCombItems()
{
this.cmbHouXuan.Items.Clear();//移除原有数据
Randomrd=newRandom();
for(inti=0;i<10;i++)//生成10个新数据
{
stringitem=string.Format("Item-{0:
X8}",rd.Next());
this.cmbHouXuan.Items.Add(item);//添加到ComboBox中
}
this.cmbHouXuan.SelectedIndex=0;//默认选中第一条
}
//重新生成ComboBox中侯选项
privatevoidbtnFresh_Click(objectsender,EventArgse)
{
this.GenerateCombItems();//重新生成CombBox中候选项
}
//将CombBox中选中值¦添加到ListBox中D
privatevoidbtnAddOne_Click(objectsender,EventArgse)
{
//经过ComboBox.SelectedItem获取目前选中候选项,然后添加到ListBox中D
stringitem=(string)this.cmbHouXuan.SelectedItem;
this.lstResults.Items.Add(item);
}
//从ListBox中移除目前选中项
privatevoidbtnRemoveOne_Click(objectsender,EventArgse)
{
if(this.lstResults.SelectedIndex>=0)//假如目前ListBox中有选中条目,移除它
{
this.lstResults.Items.RemoveAt(this.lstResults.SelectedIndex);
}
}
//从ListBox中移除全部项
privatevoidbtnRemovAll_Click(objectsender,EventArgse)
{
this.lstResults.Items.Clear();
}
}
例5-10:
源代码:
privatevoidbtnMsgBox_Click(objectsender,EventArgse)
{
MessageBox.Show("这是第一个消息框,只有确定按钮");
//显示最简单MessageBox
MessageBox.Show("这是二个消息框¨,有标题,只有确定按钮","第二个消息框");
//显示有文本和标题MessageBox
//显示含有文本、标题、确定和取消按钮MessageBox
MessageBox.Show("这是第三个消息框¨,有标题,只有确定和¨取消按钮",
"第三个消息框",MessageBoxButtons.OKCancel);
//显示含有文本、标题、确定和¨取消按钮、告警图标MessageBox
MessageBox.Show("这是第四个消息框¨,有标题,只有确定和取消按钮,告警图标",
"第四个消息框",MessageBoxButtons.OKCancel,MessageBoxIcon.
Warning);
}
例5-11:
源代码:
privatevoidbtnOpenFile_Click(objectsender,EventArgse)
{
OpenFileDialogofdlg=newOpenFileDialog();
//创建OpenFileDialog对象
ofdlg.Filter="文本文件(*.txt)|*.TXT|Word文件(*.doc)|*.DOC";//只选择TXT和DOC扩展名文件
ofdlg.Title="选择文本文件或Word文件";//设置对话框标题
if(ofdlg.ShowDialog()==DialogResult.OK)//显示对话框,并等候返回
{
this.tbOpenFileName.Text=ofdlg.FileName;//假如用户选择了文件则显示到界面
}
else
{
this.tbOpenFileName.Text="还没有选择要打开文件";//没有选择文件,则显示默认提醒
}
}
例5-12:
源代码:
privatevoidbtnSetColor_Click(objectsender,EventArgse)
{
ColorDialogcdlg=newColorDialog();//创建ColorDialog对象
cdlg.Color=btnSetColor.ForeColor;//设置默认颜色为btnSetColor目前前景色
if(cdlg.ShowDialog()==DialogResult.OK)//显示对话框,并等候返回
{
this.btnSetColor.ForeColor=cdlg.Color;//选择了新颜色,则更新btnSetColor前景色
}
}
例5-13:
源代码:
privatevoidbtnSetFont_Click(objectsender,EventArgse)
{
FontDialogfdlg=newFontDialog();//创建FontDialog对象
fdlg.Font=btnSetFont.Font;//设置默认字体为btnSetFont目前字体
if(fdlg.ShowDialog()==DialogResult.OK)//显示对话框,并等候返回
{
this.btnSetFont.Font=fdlg.Font;//选择了新字体,则更新btnSetFont字体
}
}
四、试验总结
经过此次试验,我学会了在VisualStudio中创建和运行窗体程序;也掌握了Windows窗体基础操作;学习使用了常见Windows控件;同时学会了使用菜单和工具栏以及通用对话框。