控件使用 实验报告Word格式文档下载.docx
《控件使用 实验报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《控件使用 实验报告Word格式文档下载.docx(15页珍藏版)》请在冰点文库上搜索。
点击取消按钮结束程序运行。
源代码:
在窗体form1里面添加如下代码:
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)
MessageBox.Show(this.textBox1.Text+"
欢?
迎®
-你?
!
ê
?
"
"
提¬
¨
¢
示º
消?
息¡
é
MessageBoxButtons.OK,MessageBoxIcon.Information);
privatevoidbutton2_Click(objectsender,EventArgse)
this.Close();
}
2.文本框,标签和按钮练习
点击红色按钮,将文本框中的文字颜色变成红色,同时把该按钮变成不可用(提示:
修改按钮的Enabled属性为false使其不可用);
点击黑色按钮,将文本框中的文字颜色变成黑色,同时把该按钮变成不可用;
点击“文本内容复制到标签”将文本框内容复制到下方标签。
namespaceshiyan5_2
textBox1.ForeColor=System.Drawing.Color.Red;
this.button1.Enabled=false;
textBox1.ForeColor=System.Drawing.Color.Black;
this.button2.Enabled=false;
privatevoidbutton3_Click(objectsender,EventArgse)
label1.Text=textBox1.Text;
3.定时器练习
在窗体中显示字符,每隔1秒字符移动一定距离,先右移,移到右边界,再左移,移到左边界,又一次右移,如此循环。
(提示:
修改标签的Left属性值)
namespaceshiyan5_3
privateboolMoveToRight=true;
privatevoidtimer1_Tick(objectsender,EventArgse)
intx=label1.Location.X;
inty=label1.Location.Y;
intincrement=10;
if(MoveToRight)
if((x+label1.Size.Width)+increment>
=this.ClientRectangle.Width)MoveToRight=false;
else
label1.Location=newPoint(x+increment,y);
if(!
MoveToRight)
if(x-increment<
=0)
MoveToRight=true;
label1.Location=newPoint(x-increment,y);
4.复选框、单选框、组合框练习
实现如图所示的功能。
要求对相应控件做了设置之后,马上见到对应的字体效果。
清除按钮用来清除文本框中的文字内容,退出按钮用来退出程序。
namespaceshiyan5_4
textBox1.Text="
;
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
textBox1.Font=newSystem.Drawing.Font("
宋?
体¬
15f);
privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)
楷?
15f);
privatevoidradioButton4_CheckedChanged(objectsender,EventArgse)
黑¨
²
privatevoidradioButton3_CheckedChanged(objectsender,EventArgse)
隶¢
£
¤
书º
¦
privatevoidcheckBox1_CheckedChanged(objectsender,EventArgse)
if(checkBox1.Checked)
textBox1.Font=newFont("
15f,FontStyle.Bold);
privatevoidcheckBox2_CheckedChanged(objectsender,EventArgse)
if(checkBox2.Checked)
15f,FontStyle.Italic);
privatevoidcheckBox3_CheckedChanged(objectsender,EventArgse)
if(checkBox3.Checked)
15f,FontStyle.Underline);
privatevoidcheckBox4_CheckedChanged(objectsender,EventArgse)
if(checkBox4.Checked)
15f,FontStyle.Strikeout);
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)
comboBox1.SelectedItem=newFont("
textBox1.Font.Size);
5.列表框练习
实现程序如下功能:
在文本框中输入水果,点击添加按钮将输入的水果添加到列表框中,如果列表框中已存在此种水果则不添加;
在列表框中选中某些水果后点击删除,删除这些果;
点击清除按钮将列表框中的所有水果都清除。
namespaceshiyan5_5
{publicpartialclassTestList:
{publicTestList()
{InitializeComponent();
privatevoidAddButton_Click(objectsender,EventArgse)
{boolrepeatJudege=false;
if(textBox.Text.Trim()!
="
)
{foreach(stringiteminFruitListBox.Items)
{if(textBox.Text==item)
{MessageBox.Show("
列表已有"
+textBox.Text,"
错误提醒"
MessageBoxButtons.OK,MessageBoxIcon.Error);
repeatJudege=true;
break;
repeatJudege)
FruitListBox.Items.Add(textBox.Text.Trim());
textBox.Text="
else
MessageBox.Show("
输入不能为空值"
"
MessageBoxButtons.OK,MessageBoxIcon.Error);
privatevoidDelButton_Click(objectsender,EventArgse)
{intcount=FruitListBox.SelectedItems.Count;
if(count!
{for(inti=0;
i<
count;
i++)
{FruitListBox.Items.Remove(FruitListBox.SelectedItems[0]);
请选中删除项"
privatevoidDelAllButton_Click(objectsender,EventArgse)
{if(FruitListBox.Items.Count!
=0)
FruitListBox.Items.Clear();
表中为空"
privatevoidTestList_Load(objectsender,EventArgse)
{}
6.简单记事本。
使用菜单方式实现字体选择和文字颜色的选择;
使用工具栏方式实现字体选择和文字颜色的选择;
使用状态栏显示当前输入的文字的个数信息。
namespaceshiyan5_6
{publicpartialclassForm1:
{publicForm1()
privatevoid字体选择ToolStripMenuItem_Click(objectsender,EventArgse)
{FontDialogfd=newFontDialog();
if(fd.ShowDialog()==DialogResult.OK)
{textBox1.Font=fd.Font;
privatevoidForm1_Load(objectsender,EventArgse)
{toolStripStatusLabel2.Text="
0"
privatevoidtoolStripButton1_Click(objectsender,EventArgse)
privatevoid则提示颜色ToolStripMenuItem_Click(objectsender,EventArgse)
{ColorDialogfd=newColorDialog();
{textBox1.ForeColor=fd.Color;
privatevoidtoolStripButton2_Click(objectsender,EventArgse)
privatevoidtextBox1_TextChanged(objectsender,EventArgse)
{toolStripStatusLabel2.Text=textBox1.Text.Length.ToString();
7.简单图片查看器。
要求使用PictureBox和ImageList控件。
namespaceshiyan5_7
{if(label1.Text=="
1"
{btback.Enabled=false;
pictureBox1.Image=imageList1.Images[0];
inti=0;
privatevoidbtback_Click(objectsender,EventArgse)
{btnext.Enabled=true;
if(label1.Text!
{i--;
label1.Text=(Convert.ToInt16(label1.Text)-1).ToString();
pictureBox1.Image=imageList1.Images[i];
if(i==0)
btback.Enabled=false;
privatevoidbtnext_Click(objectsender,EventArgse)
{btback.Enabled=true;
6"
{i++;
label1.Text=(Convert.ToInt16(label1.Text)+1).ToString();
pictureBox1.Image=imageList1.Images[i];
if(i==5)
btnext.Enabled=false;
实验总结(结论或问题分析):
通过本次实验,进一步熟悉了窗体的概念知识,对于窗体的常用属性有了一定程度的了解,学会了文本操作类控件中的标签控件和文本控件的使用方法,加深了对选择操作类控件中的复选框、单选框、列表框及组合框的使用方法的理解。
实验成绩
任课教师签名