C#—实验10.4

来源:互联网 发布:网络配线架品牌 编辑:程序博客网 时间:2024/05/16 18:58

设计系统的登录窗体和主窗体:登录窗体(frmLogin)、一个MDI主窗体(frmMain)、两个子窗体(frm子窗体1、frm子窗体2)。要求:

在登录窗体中输入用户名和密码,正确后,才可进入MDI主窗体;通过菜单命令关闭主窗体。

具体要求:

(1)做法:先创建四个窗体,按上面要求修改各自的名称;

(2)登录窗体:大小不可调;显示时位于屏幕中央;没有最大化,最小化,关闭按钮。在登录窗体中,假设用户输入账号为“zhangsan",且密码为"123"(密码显示为*),

则通过登录,销毁登录窗体,同时打开主窗体。否则给出错误提示,如:“账号或密码错误”。

Form1:

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class frmLogin : Form    {        public frmLogin()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            if (textBox2.Text == "123")                this.DialogResult = DialogResult.OK;            else            {                DialogResult dr = MessageBox.Show("密码错误", "密码验证", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);                if (dr == DialogResult.Retry)                {                    textBox2.Text = "";                    textBox2.Focus();                }            }          }        private void button2_Click(object sender, EventArgs e)        {            Application.Exit();        }    }}

Form2:

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class frmMain : Form    {        public frmMain()        {            InitializeComponent();        }        private void 关闭窗体ToolStripMenuItem_Click(object sender, EventArgs e)        {            Application.Exit();        }    }}

Program.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication2{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            frmLogin loginFrm = new frmLogin();            loginFrm.ShowDialog();            if (loginFrm.DialogResult == DialogResult.OK)                Application.Run(new frmMain());//指定启动窗体          }    }}

修改基本属性:

将登录窗体的StartPosition设置为“CenterScreen”;

将登录窗体的FormBorderStyle设置为“None";


运行结果:

密码输入正确后,进入主窗体:



0 0
原创粉丝点击