我现在做个小的管理软件,父窗口上面有很多功能按钮,比如说新增、修改,我现在想实现winform中,当点击新增打开新增子窗口,当点击修改的时候,需要打开修改的子窗体同时关闭新增的子窗口,该如何实现呢?

解决方案 »

  1.   

    使用单例模式,然后判断,如果修改的子窗口打开,那么就Close。
      

  2.   

    使用单例模式,然后判断,如果修改的子窗口打开,那么就Close。
      

  3.   

    你在打开所有子窗口时候,都要fm.Show(this);然后用如下方法打开新窗口,关闭旧窗口            Form[] f= this.OwnedForms;
                foreach (Form ff in f)
                    ff.Close();
                Form2 fm = new Form2();
                fm.Show(this);
      

  4.   

    新增窗口使用单例模式
    //AddForm新增窗口
     public partial class AddForm : Form
        {
            private static object lockHelper = new object();
            private static UpdateForm instance = null;
            private  AddForm()
            {
                InitializeComponent();
            }        public static AddForm CreateInstance()
            {
                if (instance == null)
                {
                    lock (lockHelper)
                    {
                        if (instance == null)
                        {
                            instance = new AddForm();
                            //加上实例关闭事件,窗体就会自动回收,即instance=null;
                            instance.FormClosed += new FormClosedEventHandler(DestroyForm);                    }
                        return instance;
                    }
                }
                else
                {
                    return instance;
                }
            }        /// <summary>
            /// 当窗体关闭时将Instance置空
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private static void DestroyForm(object sender, FormClosedEventArgs e)
            {
                instance = null;
            }    }修改窗口调用
    public partial class UpdateForm : Form
        {
            AddForm addForm = null;
            public UpdateForm()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                addForm = AddForm.CreateInstance();
                if(addForm!=null)
                addForm.Show();
            }        private void button2_Click(object sender, EventArgs e)
            {
                addForm.Close();
            }
        }
      

  5.   

    要是我没领悟错的话,兄弟是否想达到"菜单式"进入程序的界面.俺这驴是这样实现的:把各个页面都做好后,比如说A,B,C三个窗体.再多做一个E窗体;E窗体里有一个Listbox,两个button(一个确认,一个退出);Listbox里编辑好A,B,C,D的名称;实现的效果是,当点击Listbox里的A,就弹出A窗体.点A窗体的"返回",就返回到E窗体,
               当点击Listbox里的B,就弹出A窗体.点B窗体的"返回",就返回到E窗体,
               ......这是俺这驴以前写的一个示例,里边可能有一些没多关要的东西;
    代码如下: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;
    using System.Runtime.InteropServices;//实现窗体效果的命名空间
    using System.Collections;
    using System.IO;
    using System.Data.OracleClient;
    using System.Text.RegularExpressions;
    namespace webcam
    {
        public partial class NewLogin : Form
        {
            public NewLogin()
            {
                InitializeComponent();
            }       
            [DllImport("user32.dll")]
            private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
            private const int AW_HOR_POSITIVE = 0x0001;//从左到右打开窗口
            private const int AW_HOR_NEGATIVE = 0x0002;//从右到左打开窗口
            private const int AW_VER_POSITIVE = 0x0004;//从上到下打开窗口
            private const int AW_VER_NEGATIVE = 0x0008;//从下到上打开窗口
            private const int AW_CENTER = 0x0010;//从中央打开
            private const int AW_HIDE = 0x10000;//隐藏窗体
            private const int AW_ACTIVATE = 0x20000;//显示窗体
            private const int AW_SLIDE = 0x40000;
            private const int AW_BLEND = 0x80000;//淡入淡出效果        private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }       private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.ExitThread();
                AnimateWindow(this.Handle, 200, AW_HIDE | AW_VER_NEGATIVE);
            }        private void button1_Click(object sender, System.EventArgs e)
            {            if (listBox1.SelectedIndex == 0)
                { this.Hide(); new webcam.AConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 1)
                { this.Hide(); new webcam.BConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 2)
                { this.Hide(); new webcam.CConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 3)
                { this.Hide(); new webcam.DConsoleForm().ShowDialog(); }
                listBox1.Focus();
            }       
            private void listBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                   if (listBox1.SelectedIndex == 0)
                { this.Hide(); new webcam.AConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 1)
                { this.Hide(); new webcam.BConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 2)
                { this.Hide(); new webcam.CConsoleForm().ShowDialog(); }
                if (listBox1.SelectedIndex == 3)
                { this.Hide(); new webcam.DConsoleForm().ShowDialog(); }
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.ExitThread();
                AnimateWindow(this.Handle, 200, AW_HIDE | AW_VER_NEGATIVE);
            }        private void Form1_Load(object sender, EventArgs e)
            {
                AnimateWindow(this.Handle, 200, AW_ACTIVATE | AW_VER_POSITIVE);//200指明动画持续的时间(以微秒计),完成一个动画的标准时间为200微秒。        }    }
    }