控件使用实验报告.docx
《控件使用实验报告.docx》由会员分享,可在线阅读,更多相关《控件使用实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
![控件使用实验报告.docx](https://file1.bdocx.com/fileroot1/2022-10/12/bf67d043-1a0a-437b-bf3d-4a94555bc24c/bf67d043-1a0a-437b-bf3d-4a94555bc24c1.gif)
控件使用实验报告
实验报告
课程名称
面向对象程序设计
实验名称
实验五、控件使用
日期
学生学号
姓名
班级
实验目的:
1•掌握窗体的常用属性的使用。
2.掌握文本操作类控件中的标签控件和文本控件的使用。
3.掌握选择操作类控件中的复选框、单选框、列表框、组合框的使用。
实验要求:
1.认真阅读、掌握和本实验相关的教材内容。
2.设计并编写代码完成题目要求的任务。
3.撰写实验报告。
实验内容与步骤:
1.多窗体练习
实现如图所示功能。
输入用户名后点击登录按钮,弹出右图消息框。
要求在消息框中显示用户输入的用户名和“欢迎你”语句。
点击取消按钮结束程序运行。
源代码:
在窗体forml里面添加如下代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceshiyan5
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
MessageBoxShow(this.textBox1.Text+"欢?
迎?
-你?
!
e?
""提示o?
消?
息e"MessageBoxButtons.OK,MessageBoxIcon.lnformation);
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.Close();
}
}
2.文本框,标签和按钮练习
实现如图所示功能。
点击红色按钮,将文本框中的文字颜色变成红色,同时把该按钮变成不可用(提示:
修改按钮的Enabled属性为false使其不可用);点击黑色按钮,将文本框中的文字颜色变成黑色,同时把该按钮变成不可用;点击“文本内容复
制到标签”将文本框内容复制到下方标签。
源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceshiyan5_2
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
EventArgse)
privatevoidbutton1_Click(objectsender,
{
textBox1.ForeColor=System.Drawing.Color.Red;
this.button1.Enabled=false;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
textBox1.ForeColor=System.Drawing.Color.Black;
this.button2.Enabled=false;
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
label1.Text=textBox1.Text;
}
}
}
3•定时器练习
在窗体中显示字符,每隔1秒字符移动一定距离,先右移,移到右边界,再左移,移到左边界,又一次右移,如此循环。
(提示:
修改标签的Left属性值)
源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceshiyan5_3
{
publicpartialclassForm1:
Form
{
publicForm1()
InitializeComponent();
privateboolMoveToRight=true;
privatevoidtimer1_Tick(objectsender,EventArgse)
{
intx=label1.Location.X;inty=labell.Location.Y;intincrement=10;if
(MoveToRight)
if((x+labell.Size.Width)+increment>=this.ClientRectangle.Width)MoveToRight
=false;
else
labell.Location=newPoint(x+increment,y);if(!
MoveToRight)
if(x-increment<=0)
MoveToRight=true;
else
labell.Location=newPoint(x-increment,y);
}
}
}
4•复选框、单选框、组合框练习
实现如图所示的功能。
要求对相应控件做了设置之后,马上见到对应的字体效果。
清除按钮用来清除文本框中的文字内容,退出按钮用来退出程序。
町复选匡丝选框.矩合桂【F冋IYW
源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceshiyan5_4
{
publicpartialclassForml:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.Close();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
textBox1.Text="";
}
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
{
textBox1.Font=newSystem.Drawing.Font(”宋?
体?
?
",15f);
}
privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)
{
textBox1.Font=newSystem.Drawing.Font(”楷?
体?
?
",15f);
}
privatevoidradioButton4_CheckedChanged(objectsender,EventArgse)
{
textBox1.Font=newSystem.Drawing.Font(”黑T体?
?
",15f);
}
privatevoidradioButton3_CheckedChanged(objectsender,EventArgse)
{
textBox1.Font=newSystem.Drawing.Font(”隶0£书o"|",15f);
}
privatevoidcheckBox1_CheckedChanged(objectsender,EventArgse)
{
if(checkBox1.Checked)
textBox1.Font=newFont("宋?
体?
?
",15f,FontStyle.Bold);
}
privatevoidcheckBox2_CheckedChanged(objectsender,EventArgse)
{
if(checkBox2.Checked)
textBox1.Font=newFont("宋?
体?
?
",15f,FontStyle.ltalic);
}
privatevoidcheckBox3_CheckedChanged(objectsender,EventArgse)
{
if(checkBox3.Checked)
textBoxl.Font=newFont("宋?
体?
?
",15f,FontStyle.Underline);
}
privatevoidcheckBox4_CheckedChanged(objectsender,EventArgse)
{
if(checkBox4.Checked)
textBox1.Font=newFont("宋?
体?
?
",15f,FontStyle.Strikeout);
}
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)
{
comboBox1.Selectedltem=newFont("宋?
体?
?
",textBox1.Font.Size);
}
}
}
5•列表框练习
实现程序如下功能:
在文本框中输入水果,点击添加按钮将输入的水果添加到列表框中,如果列表框中已存在此种水果则不添加;在列表框中选中某些水果后点击删除,删除这些果;点击清除按钮将列表框中的所有水果都清除。
口t列表眶[匸丨回
请输入你盲爱的水果:
香蕉
源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceshiyan5_5
{publicpartial