实验六 windows编程.docx
《实验六 windows编程.docx》由会员分享,可在线阅读,更多相关《实验六 windows编程.docx(46页珍藏版)》请在冰豆网上搜索。
![实验六 windows编程.docx](https://file1.bdocx.com/fileroot1/2023-2/3/91c5415a-569e-48a1-9844-dbf06a19a6d0/91c5415a-569e-48a1-9844-dbf06a19a6d01.gif)
实验六windows编程
贺州学院
C#程序设计与开发实战实验报告
班级:
14软件2
姓名:
学号
实验名称:
实验六Windows编程
完成时间
2016/5/31
一、实验目的
1.掌握窗体的常用属性和方法的使用。
2.掌握文本操作类控件中的标签控件和文本控件的使用。
3.掌握选择操作类控件中的复选框、单选框、列表框、组合框的使用。
二、实验内容
1.试编写Windows应用程序,完成下列要求:
(1)Form1(登陆窗口)和Form2窗体设计界面如下:
注意Form1窗口的外形设置。
(2)应用程序从Form1启动,输入用户名和密码,要求:
密码框以字符“#”代替用户输入显示;
(3)当用户单击Form1中的“登陆”按钮时,弹出Form2窗体,并将用户输入的用户名和密码传递到Form2的只读textBox中显示;
(4)当用户单击Form2中的“返回”按钮时,关闭Form2窗体,并将Form1窗体中的两个textBox清空;
(5)当单击Form1的取消时,结束整个程序的运行。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTest1
{
publicpartialclassForm1:
Form
{
publicForm1()
{
this.StartPosition=FormStartPosition.CenterScreen;//窗口居中显示
InitializeComponent();
}
//登录按钮
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(textBox1.Text=="")
{
MessageBox.Show("请输入用户名!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(textBox2.Text=="")
{
MessageBox.Show("请输入密码!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
Form2f2=newForm2();//定义from2窗口的对象
f2.user=textBox1.Text;//记住当前用户名
f2.pass=textBox2.Text;//记住当前密码
f2.Parent=this;
this.Hide();//隐藏父窗口
f2.Show();//显示子窗口
}
//取消按钮
privatevoidbutton2_Click(objectsender,EventArgse)
{
if(DialogResult.Yes==MessageBox.Show("是否退出?
","提示信息",MessageBoxButtons.YesNo))
{
this.Close();
}
}
//窗口活动时执行的代码
privatevoidForm1_Activated(objectsender,EventArgse)
{
textBox1.Text="";
textBox2.Text="";
textBox1.Focus();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTest1
{
publicpartialclassForm2:
Form
{
publicForm1Parent;//父窗口
publicStringuser,pass;//用户名、密码
publicForm2()
{
this.StartPosition=FormStartPosition.CenterScreen;//窗口居中显示
InitializeComponent();
}
//返回按钮
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(DialogResult.Yes==MessageBox.Show("是否返回?
","提示信息",MessageBoxButtons.YesNo))
{
this.Parent.Show();//显示父窗口
this.Close();//关闭子窗口
}
}
//加载窗口
privatevoidForm2_Load(objectsender,EventArgse)
{
textBox1.Text=user;//显示用户名
textBox2.Text=pass;//显示密码
}
//关闭窗口
privatevoidForm2_FormClosed(objectsender,FormClosedEventArgse)
{
this.Parent.Show();//显示父窗口
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindows1
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidtextBox2_TextChanged(objectsender,EventArgse)
{
}
privatevoidtextBox1_TextChanged(objectsender,EventArgse)
{
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Form2frm2=newForm2();//实例化Form2
frm2.user=textBox1.Text;
frm2.password=textBox2.Text;
frm2.myparent=this;
this.Hide();
if(textBox1.Text=="aaa"&&textBox2.Text=="123456")
frm2.Show();//调用Show方法显示Form2窗体
else
{
MessageBox.Show("密码或者用户名不正确,请重新输入");
textBox1.Text="";
textBox2.Text="";
textBox1.Focus();
}
}
privatevoidForm1_Load(objectsender,EventArgse)
{
textBox2.MaxLength=6;
textBox2.PasswordChar='#';
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
textBox1.Text="";
textBox2.Text="";
textBox1.Focus();
}
privatevoidForm1_Activated(objectsender,EventArgse)
{
textBox1.Text="";
textBox2.Text="";
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindows1
{
publicpartialclassForm2:
Form
{
publicstringuser,password;
publicFormmyparent;
publicForm2()
{
InitializeComponent();
}
privatevoidForm2_Load(objectsender,EventArgse)
{
textBox1.Text=user;
textBox2.Text=password;
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
this.Close();
this.myparent.Show();
}
privatevoidForm2_FormClosed(objectsender,FormClosedEventArgse)
{
this.myparent.Show();
}
}
}
2.试编写Windows应用程序,完成下列要求:
(1)Form2(登陆窗口)和Form1(我的应用程序)窗体设计界面如下。
注意Form2窗口的外形设置,另外主方法中的Application.Run(newForm1());不能修改。
(2)运行应用程序时弹出登录窗口,输入用户名和密码,要求:
密码框以字符“*”代替用户输入显示(注意登录窗体要求显示在屏幕中间);
(3)当用户单击Form2中的“登陆”按钮时,如果用户名和密码都正确,就关闭登录窗口并弹出Form1窗体,并将用户输入的用户名和密码传递到Form1的richTextBox1分行显示;如果密码不正确,则弹出消息框显示错误信息,并提示重新输入;
(4)当用户单击“取消”按钮时,结束整个程序的运行;
(5)当用户关闭Form1时,结束整个程序的运行。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTest2
{
publicpartialclassForm2:
Form
{
publicForm2()
{
this.StartPosition=FormStartPosition.CenterScreen;//窗口居中显示
InitializeComponent();
}
//登录按钮
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(textBox1.Text=="")
{
MessageBox.Show("请输入用户名!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(textBox2.Text=="")
{
MessageBox.Show("请输入密码!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
//判断用户名和密码是否正确
if(textBox1.Text=="lml"&&textBox2.Text=="123456")
{
Form1.user=textBox1.Text;
Form1.pass=textBox2.Text;
this.Close();//关闭登录窗口
}
else
{
MessageBox.Show("用户名或密码输入不正确,请重新输入!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox1.Text="";
textBox2.Text="";
textBox1.Focus();
}
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
if(DialogResult.Yes==MessageBox.Show("是否退出?
","提示信息",MessageBoxButtons.YesNo))
{
Application.Exit();//退出整个应用程序
}
}
privatevoidForm2_Load(objectsender,EventArgse)
{
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTest2
{
publicpartialclassForm1:
Form
{
publicstaticstringuser,pass;//显示用户名、密码
publicForm1()
{
this.StartPosition=FormStartPosition.CenterScreen;//窗口居中显示
InitializeComponent();
}
//加载窗口
privatevoidForm1_Load(objectsender,EventArgse)
{
richTextBox1.AppendText(Form1.user+"\n"+Form1.pass);//显示用户名和密码
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindows2
{
publicpartialclassForm2:
Form
{
publicForm2()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Form1frm1=newForm1();//实例化Form1
Form2frm2=newForm2();//实例化Form2
frm1.test1=textBox1.Text;
frm1.test2=textBox2.Text;
frm1.myparent=this;
this.Hide();
if(textBox1.Text=="aaa"&&textBox2.Text=="123456")
this.Close();
else
{
MessageBox.Show("密码或者用户名不正确,请重新输入");
textBox1.Text="";
textBox2.Text="";
frm2.Show();//调用Show方法显示Form2窗体
textBox1.Focus();
this.Close();
}
}
privatevoidForm2_Load(objectsender,EventArgse)
{
textBox1.MaxLength=6;
textBox2.PasswordChar='*';
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.Close();
}
privatevoidForm2_FormClosed(objectsender,FormClosedEventArgse)
{
this.Close();
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindows2
{
publicpartialclassForm1:
Form
{
publicstringtest1,test2;
publicFormmyparent;
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse)
{
//this.Close();
//this.myparent.Show();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
richTextBox1.Text=test1;
richTextBox1.Text=test2;
}
}
}
3.试编写Windows应用程序,完成下列要求:
(1)Form1窗体设计界面如下:
(2)单击“确定”按钮,可将textBox中的文字添加到label2中;
(3)label2中的文字可字体大小可切换三次(小、中、大),初始状态下字体为“小”,则“缩小字体”按钮不可用,每次单击“增大字体”按钮,可使增大字体一号,当字体增大为“大”时,“增大字体”按钮不可用,每次单击“缩小字体”按钮,可使字体缩小一号;
(4)“变换字体颜色”按钮,可使label2中的字体颜色随机改变。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTest3
{
publicpartialclassForm1:
Form
{
publicintAdd=0;//增大字体标志:
0代表小,1代表中,2代表大
publicForm1()
{
this.StartPosition=FormStartPosition.CenterScreen;//窗口居中显示
InitializeComponent();
}
//确定按钮
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(textBox1.Text=="")
{
MessageBox.Show("请输入文字!
","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(Add==0)
{
button3.Enabled=false;//设置缩小字体不可用
}
richTextBox1.Text=textBox1.Text;//显示文字
textBox1.Text="";//清空文字
textBox1.Focus();//定位输入焦点
}
//增大字体按钮
privatevoidbutton2_Click(objectsender,EventArgse)
{
richTextBox1.SelectAll();//全选文字
//richTextBox1.Select(0,0);//取消全选文字
if(richTextBox1.Text=="")
{
MessageBox.Show("当前文本框无文字","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
richTextBox1.Selectio