贺州学院
C#程序设计与开发实战实验报告
班级: 实验名称: 一、实验目的
1.掌握窗体的常用属性和方法的使用。
2. 掌握文本操作类控件中的标签控件和文本控件的使用。
3.掌握选择操作类控件中的复选框、单选框、列表框、组合框的使用。 二、实验内容
1. 试编写Windows应用程序,完成下列要求:
(1)Form1(登陆窗口)和Form2窗体设计界面如下:注意Form1窗口的外形设置。
14软件2 姓名: 学号 完成时间 2016/5/31 实验六 Windows编程
(2)应用程序从Form1启动,输入用户名和密码,要求:密码框以字符“#”代替用户输入显示;
(3)当用户单击Form1中的“登陆”按钮时,弹出Form2窗体,并将用户输入的用户名和密码传递到Form2的只读textBox中显示;
(4)当用户单击Form2中的“返回”按钮时,关闭Form2窗体,并将Form1窗体中的两个textBox清空;
(5)当单击Form1的取消时,结束整个程序的运行。
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 Test1 {
public partial class Form1 : Form
{
public Form1() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }
//登录按钮
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == \ {
MessageBox.Show(\请输入用户名!\提示信息\
return; }
if (textBox2.Text == \ {
MessageBox.Show(\请输入密码!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; }
Form2 f2 = new Form2(); //定义from2窗口的对象 f2.user = textBox1.Text; //记住当前用户名 f2.pass = textBox2.Text; //记住当前密码 f2.Parent = this;
this.Hide(); //隐藏父窗口
f2.Show(); //显示子窗口 }
//取消按钮
private void button2_Click(object sender, EventArgs e) {
if (DialogResult.Yes == MessageBox.Show(\是否退出?\\提示信息\MessageBoxButtons.YesNo))
{ this.Close(); } }
//窗口活动时执行的代码
private void Form1_Activated(object sender, EventArgs e) {
textBox1.Text = \ textBox2.Text = \ textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e) {
} } }
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 Test1 {
public partial class Form2 : Form {
public Form1 Parent; //父窗口 public String user, pass; //用户名、密码 public Form2() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }
//返回按钮
private void button1_Click(object sender, EventArgs e) {
if (DialogResult.Yes == MessageBox.Show(\是否返回?\\提示信息\MessageBoxButtons.YesNo))
{
this.Parent.Show(); //显示父窗口 this.Close(); //关闭子窗口 } }
//加载窗口
private void Form2_Load(object sender, EventArgs e) {
textBox1.Text = user; //显示用户名 textBox2.Text = pass; //显示密码
}
//关闭窗口
private void Form2_FormClosed(object sender, FormClosedEventArgs e) {
this.Parent.Show(); //显示父窗口 } } }
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; namespace Windows1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void textBox2_TextChanged(object sender, EventArgs e) { }
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e) {
Form2 frm2 = new Form2(); //实例化Form2 frm2.user = textBox1.Text; frm2.password = textBox2.Text; frm2.myparent = this; this.Hide();
if (textBox1.Text == \ frm2.Show(); //调用Show方法显示Form2窗体 else {
MessageBox.Show(\密码或者用户名不正确,请重新输入\ textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); } }
private void Form1_Load(object sender, EventArgs e) {
textBox2.MaxLength = 6;
textBox2.PasswordChar = '#'; }
private void button2_Click(object sender, EventArgs e) {
textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); }
private void Form1_Activated(object sender, EventArgs e) {
textBox1.Text = \ textBox2.Text = \ } } }
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; namespace Windows1 {
public partial class Form2 : Form {
public string user, password; public Form myparent; public Form2() {
InitializeComponent(); }
private void Form2_Load(object sender, EventArgs e)