{
textBox1.Text = user; textBox2.Text = password; }
private void button1_Click(object sender, EventArgs e) {
this .Close(); this .myparent.Show(); }
private void Form2_FormClosed(object sender, FormClosedEventArgs e) {
this.myparent.Show(); } } }
2. 试编写Windows应用程序,完成下列要求:
(1)Form2(登陆窗口)和Form1(我的应用程序)窗体设计界面如下。注意Form2窗口的外形设置,另外主方法中的Application.Run(new Form1());不能修改。
(2)运行应用程序时弹出登录窗口,输入用户名和密码,要求:密码框以字符“*”代替用户输入显示(注意登录窗体要求显示在屏幕中间);
(3)当用户单击Form2中的“登陆”按钮时,如果用户名和密码都正确,就关闭登录窗口并弹出Form1窗体,并将用户输入的用户名和密码传递到Form1的richTextBox1分行显示;如果密码不正确,则弹出消息框显示错误信息,并提示重新输入;
(4)当用户单击“取消”按钮时,结束整个程序的运行; (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 Test2 {
public partial class Form2 : Form {
public Form2() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }
//登录按钮
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == \ {
MessageBox.Show(\请输入用户名!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; }
if (textBox2.Text == \ { MessageBox.Show(\请输入密码!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; }
//判断用户名和密码是否正确
if (textBox1.Text == \ {
Form1.user = textBox1.Text; Form1.pass = textBox2.Text;
this.Close(); //关闭登录窗口 } else {
MessageBox.Show(\用户名或密码输入不正确,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); }
}
private void button2_Click(object sender, EventArgs e) {
if (DialogResult.Yes == MessageBox.Show(\是否退出?\\提示信息\MessageBoxButtons.YesNo))
{
Application.Exit(); //退出整个应用程序
} }
private void Form2_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 Test2 {
public partial class Form1 : Form {
public static string user, pass; //显示用户名、密码 public Form1() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }
//加载窗口
private void Form1_Load(object sender, EventArgs e) {
richTextBox1.AppendText(Form1.user+\ //显示用户名和密码
} } }
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 Windows2 {
public partial class Form2 : Form {
public Form2() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
Form1 frm1 = new Form1(); //实例化Form1 Form2 frm2 = new Form2(); //实例化Form2 frm1.test1 = textBox1.Text; frm1.test2 = textBox2.Text; frm1.myparent = this; this.Hide();
if (textBox1.Text == \this.Close(); else {
MessageBox.Show(\密码或者用户名不正确,请重新输入\ textBox1.Text = \ textBox2.Text = \
frm2.Show(); //调用Show方法显示Form2窗体 textBox1.Focus(); this.Close(); } }
private void Form2_Load(object sender, EventArgs e) {
textBox1.MaxLength = 6;
textBox2.PasswordChar = '*'; }
private void button2_Click(object sender, EventArgs e) { this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e) { this.Close();
} } }
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 Windows2 {
public partial class Form1 : Form {
public string test1,test2; public Form myparent; public Form1() {
InitializeComponent(); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
//this.Close();
//this.myparent.Show(); }
private void Form1_Load(object sender, EventArgs e) {
richTextBox1.Text = test1; richTextBox1.Text = test2; } } }
3. 试编写Windows应用程序,完成下列要求:
(1)Form1窗体设计界面如下: