用C#写的单例模式--很好理解,拿来即可用
更新:HHH   时间:2023-1-7


 

 

 

很容易的操作。先建两个窗体(form),一个作为另一个的父窗体。

即把父窗体的属性ISMdiContainer设置为TRUE;即可成为父窗体。

然后把这段代码写进父窗体里

 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateUserMenu();
        }
        private void ChangeChildren(Form frm)
        {
            if (this.MdiChildren.Length ==1)
            {
                frm.Show();
                return;
            }
            foreach (Form children in this.MdiChildren)
            {
                if (frm == children)
                {
                    children.Activate();
                    break;
                }
                else
                {
                    children.Close();
                    frm.Show();
                    break;
                }
            }
        }

        private void CreateUserMenu()
        {
            ToolStripMenuItem tool= new ToolStripMenuItem();
            tool.Name = "tool";
            tool.Image = Image.FromFile(Application.StartupPath + "\\p_w_picpaths\\wallet.png");
            this.menuStrip1.Items.Add(tool);
            tool.Click += new EventHandler(UpdateTool_Click);
        }

        private void UpdateTool_Click(object sender, EventArgs e)
        {
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                f2.Text = "工具";
                ChangeChildren(f2);
        }
    }
}

有些是事件,不能直接复制,需要稍作修改。

返回编程语言教程...