Click here to Skip to main content
14,332,404 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have 3 forms One is master form two other form inherit it and have same contents with some additional functions.
I want through master form Second form should open and through second form third form should open at the same time second form should get closed.
I have thought to have some global object but it give runtime exception "An unhandled exception of type 'System.StackOverflowException' occurred in sysgeo.exe"
If any body have idea please suggest.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

You don't need a global object. Instantiate an instance of Form2 in your master form and open it. Handle the Close event on Form2 and instantiate an instance of form 3 and open it. Simple, no?
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

In this situation the 'master form' should control the sub form(s) creation.
The 'second form' should raise an event such as ThirdFormRequested. The 'master form' in it's handler for this event can then close the 'second form' and open the 'third form'.

Something like this:
using System;
using System.Windows.Forms;

namespace MasterSlave
{
    public partial class FormMaster : Form
    {
        FormSlave formSlave;

        public FormMaster()
        {
            InitializeComponent();
            Shown += new EventHandler(FormMaster_Shown);
        }

        void FormMaster_Shown(object sender, EventArgs e)
        {
            CreateSlave();
        }

        private void CreateSlave()
        {
            formSlave = new FormSlave();
            formSlave.OtherSlaveRequest += new EventHandler(formSlave_OtherSlaveRequest);
            formSlave.Show();
        }

        void formSlave_OtherSlaveRequest(object sender, EventArgs e)
        {
            if (formSlave != null)
                formSlave.Close();
            CreateSlave();
        }
    }
}
using System;
using System.Windows.Forms;
namespace MasterSlave
{
    public partial class FormSlave : Form
    {
        public event EventHandler OtherSlaveRequest;
        private Button button;
        public FormSlave()
        {
            InitializeComponent();
            button = new Button();
            button.Text = "Other &Slave";
            Controls.Add(button);
            button.Click += new EventHandler(button_Click);
        }
        void button_Click(object sender, EventArgs e)
        {
            OnOtherSlaveRequest(EventArgs.Empty);
        }
        protected virtual void OnOtherSlaveRequest(EventArgs e)
        {
            EventHandler eh = OtherSlaveRequest;
            if (eh != null)
                eh(this, e);
        }
    }
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

Thanks for the suggestions guys. I found one more solution which is very much what I was looking for.
I prefer to use Parent and child concept here.
So that I can use same form master form and I can use other child forms to get open in the parent form
like as follows I will write in Form one.
Form2 obj = new Form2();
obj.MdiParent = this;
obj.Show();

Cheers,
Deepak

[Added by DaveyM69] You never mentioned you were making a MDI application!

If it's an MDI app then this is fine as the child cannot exist without the parent so the coupling of Form2 to Form1 is OK and is desirable.

If it's not then it's a poor approach as any call to this.MdiParent may potentially fail if later the form is used not as a child.[/Added]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

Actually I am new to this and was searching some solution to j=have similar menu strip for all the pages. MDI application gives this. I think I can go with this. In future may be I don't need to change the strategies .

Thanks !!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100