创建一个登录窗体(frmLogin)、一个主窗体(frmMain)、两个子窗体(frm子窗体1、frm子窗体2)。

来源:互联网 发布:678fff域名升级www47 编辑:程序博客网 时间:2024/05/27 09:49

111111111111111111111111111111111111111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    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());
            }

        }
    }
}
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 WindowsFormsApplication11
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "zhangsan" && textBox2.Text == "123")
            {
                //frmMain fm = new frmMain();
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
            else
            {
                MessageBox.Show("用户名或密码错误!");
            }
        }

        private void button2_Click(object sender, EventArgs 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.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void 打开子窗体1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //关闭所有已经打开子窗体
            foreach (var item in this.MdiChildren)
            {
                item.Dispose();
            }
            //打开指定的子窗体
            frm子窗体1 f = new frm子窗体1();
            f.Show();
            f.MdiParent = this;
        }

        private void 打开子窗体2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             //关闭所有已经打开子窗体
            foreach (var item in this.MdiChildren)
            {
                item.Dispose();
            }
            //打开指定的子窗体
            frm子窗体2 f = new frm子窗体2();
            f.Show();
            f.MdiParent = this;
        }
    }
}