* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 作 者: 刘镇
* 完成日期: 2012 年 11 月 25 日
* 版 本 号: 3.029
* 对任务及求解方法的描述部分
* 问题描述:
要求:在登录窗体中,假设当用户输入账号为“zhangsan”,且密码为“123”(密码显示为*),则通过登录,销毁登录窗体,同时打开主窗体。否则,给出错误提示,如“账号或密码错误”
要求:当用户单击“打开子窗体1”菜单项,即可打开子窗体1,且该子窗体为主窗体子窗体、自动关闭其他子窗体(即每次只打开一个子窗体)。
*代码部分:
From1.cs:
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 Win11_1
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals("zhangsan") && textBox2.Text.Equals("123"))
{
frmMain frmMain = new frmMain();
this.DialogResult = DialogResult.OK;
}
else
{
string message = "用户名错误!";
if(textBox2.Text.Equals("123") == false)
{
message = "密码错误!";
}
MessageBox.Show(message);
textBox1.Clear();
textBox2.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
}
}
Form2.cs:
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 Win11_1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
private void 打开子窗体1ToolStripMenuItem_Click(object sender, EventArgs e)
{
//关闭所有已经打开子窗体
foreach (var item in this.MdiChildren)
{
item.Dispose();
}
//打开指定的子窗体
frm1 f = new frm1();
f.Show();
f.MdiParent = this;
}
private void 打开子窗体2ToolStripMenuItem_Click(object sender, EventArgs e)
{
//关闭所有已经打开子窗体
foreach (var item in this.MdiChildren)
{
item.Dispose();
}
//打开指定的子窗体
frm2 f = new frm2();
f.Show();
f.MdiParent = this;
}
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Win11_1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmLogin f = new frmLogin();
if (f.ShowDialog() == DialogResult.OK)
{
Application.Run(new frmMain());
}
}
}
}
测试结果:
心得经验:
1、关键代码:if (f.ShowDialog() == DialogResult.OK) { Application.Run(new frmMain()); }
修改当前运行窗体;
if (textBox1.Text.Equals("zhangsan") && textBox2.Text.Equals("123"))
{ frmMain frmMain = new frmMain(); this.DialogResult = DialogResult.OK; }
当密码账户正确时如何显示;