多重窗体实验

来源:互联网 发布:node js前景 知乎 编辑:程序博客网 时间:2024/06/18 12:39

题目要求:

    设计系统的的登录窗体和主窗体:登录窗体(frmLogin),一个MDI主窗体(frmMain),两个子窗体(frm子窗体1,frm子窗体2)。要求在登录窗体中输入用户名和密码,用户名和密码正确后,才可进入MDI主窗体;通过菜单命令关闭主窗体。具体要求:。。。。


输入代码:

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace login3_1{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Form1 f = new Form1();            if (f.ShowDialog()==DialogResult.OK)            {                Application.Run(new 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 login3_1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void 打开子窗体1ToolStripMenuItem_Click(object sender, EventArgs e)        {            foreach (var item in this.MdiChildren)            {                item.Dispose();            }            window21 w = new window21();            //w.MdiParent = this;//设置当前窗体为父窗体            w.Show();        }        private void 打开子窗体2ToolStripMenuItem_Click(object sender, EventArgs e)        {            foreach (var item in this.MdiChildren)            {                item.Dispose();            }            window22 w2 = new window22();            //w2.MdiParent = this;            w2.Show();        }    }}


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 login3_1{    public partial class window21 : Form    {        string str1="hg", str2="123";        public window21()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            if (textBox2.Text == str2 && textBox1.Text == str1)            {                this.DialogResult = DialogResult.OK;                window23 w3 = new window23();                w3.Show();            }            else            {                DialogResult dr = MessageBox.Show("密码错误", "密码验证", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);                if (dr == DialogResult)                {                    Application.Exit();                    textBox2.Text = "";                    textBox2.Focus();                }                textBox2.Text = "";            }        }        private void button2_Click(object sender, EventArgs e)        {            Application.Exit();        }    }}


运行截图:





总结:

C#做应用上手的速度超乎自己的想象。。。


0 0
原创粉丝点击