form1,form2,form3三个窗体。如何实现下列功能:
第一步:form1到达某一步时显示form2,同时隐藏form1(这一步已经实现)。
第二部:form2上点击退出按钮时,弹出form3。form3上点击按钮A则form2,form3退出,同时显示FORM1;form3上点击B按钮,form3退出,form2和form1状态不变。
最好有代码。

解决方案 »

  1.   

    //Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private static Form1 instance = new Form1();        private Form1()
            {
                InitializeComponent();
            }        public static Form1 getInstance()
            {
                return instance;
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.Visible = false;
                Form2 form2 = new Form2();
                form2.Show();
            }        public Form getForm()
            {
                return this;
            }
        }
    }
    // Form2.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                //Form3 form3 = new Form3();
                //form3.ShowDialog();
                DialogResult result = MessageBox.Show("Exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Yes)
                {
                    Form1.getInstance().Visible = true;
                    this.Close();
                    this.Dispose();
                }
            }
        }
    }说明:Form1用了单例模式,我这里没有用Form3,而是改成了一个对话框,如果楼主一定要用Form3的话,也可以类似实现
      

  2.   

    补充:以下是Program.cs的代码,用单例模式是为了保证前后显示的form1是同一个
    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(Form1.getInstance());
            }
        }
    }
      

  3.   

    写个类里面有你form1,form2,form3的三个窗体对象,当你登录form1时,把类的form1=当前窗体,保存起来,
    在做form2操作的时候,直接用类点出form1就可以处理你想要的效果了
      

  4.   

    第一步:
    Form2 f2 = new Form2();
    f2.Show();
    this.Hide();
    第二部:
    1.Form2的button_click: //以非模态形式弹出Form3
      Form3 f3 = new Form3();
      f3.Show();
    2.Form3的buttonA_click:
      Application.OpenForms["Form1"].Show();
      Application.OpenForms["Form2"].Close();
      this.Close();
    3.Form3的buttonB_click:
      this.Close();
      

  5.   


    //Form1代码
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    public static Form2 f2;
    public static Form3 f3;
    public static Form1 f1;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows フォーム デザイナ サポートに必要です。
    //
    InitializeComponent(); //
    // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
    //
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(208, 40);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(72, 56);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(292, 272);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// アプリケーションのメイン エントリ ポイントです。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    f1 = new Form1();
    Application.Run(f1);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    f2 = new Form2();
    f2.Show();
    this.Hide();
    }
    }
      

  6.   


    //Form2代码,只有按钮的代码有用,就给你按钮的代码吧
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form1.f3 = new Form3();
    Form1.f3.Show();
    }//Form3代码 只是按钮代码
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form1.f2.Close();
    Form1.f3.Close();
    Form1.f1.Show();
    } private void button2_Click(object sender, System.EventArgs e)
    {
    Form1.f3.Close();
    }
      

  7.   

           private void BindView()
            {
                switch (Step)
                {
                    case 0:     
                      //数据导入类型设置
                        UcImportDataType uc = FindControl<UcImportDataType>(typeof(UcImportDataType));
                        if (uc == null)
                        {
                            uc = new UcImportDataType();
                            panel2.Controls.Add(uc);
                        }                   
                        uc.BringToFront();
                        break;
                    case 1:     
                       //数据源设置                   
                        UcDataSource ucds = FindControl<UcDataSource>(typeof(UcDataSource));
                        if (ucds == null)
                        {
                            ucds = new UcDataSource();                       
                            panel2.Controls.Add(ucds);
                        }
                        ucds.BringToFront();
                        break;         
                    case 2:  
                      //.................
                      break;              
                }
            }
    希望对你有帮助.
      

  8.   

    楼主可以参考这里的两篇文章,对于数据的交互进行了详细的讲解。Windows窗体间的数据交互
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    谈基于.net平台windows开发中的模式窗体
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
      

  9.   

    this.Visible = false;
                Form2 form2 = new Form2();
                form2.Show(); 自己理解了才重要。想怎么改就怎么改