mahesh
mahesh

Reputation: 4763

Open one child form at a time

I am currently developing stand alone application in c#, also I am new in c# so don't know all its features..

In my application I am having one main form having MDIContainer property true.. In that main form I have created one menu through which one can open different child forms...

The problem is if user clicks on menu item the form is being open as I have created its instance and shown it on click event. But if one child form is already open and user clicks on another then that also being open..

In that case I want automatically close the previous child form..

Below is sample code..

public void menu1_click(sender,e)
{
  Dim objForm1 As New Form1()
  objForm1.ShowDialog()
}

and for secode form,

public void menu2_click(sender,e)
{
Dim objForm2 As New Form2()
objForm2.ShowDialog()
}

so here both form can open at a time but i want previous one to close if new gets open.. one form at a time..

Kindly ignore syntax and punctual mistakes as its just sample.. Kindly suggest me the best way to do it!!!!

Upvotes: 0

Views: 3417

Answers (6)

waldrumpus
waldrumpus

Reputation: 2590

If you only want one child form to exist at a time, there may be other mechanisms better suited than MDI forms.

For example, instead of using child forms, you could create user controls. Display one user control in a fixed spot in your main form (e.g. inside a panel, for positioning), then swap it out for a different user control; this is instead of opening a new child window and closing the old one.

Upvotes: 0

Manish Parakhiya
Manish Parakhiya

Reputation: 3798

For closing all other forms you need to create one class and static method like following:

class Helper
{

    public static void HideAllForms()
    {
        Assembly currentAssembly = Assembly.GetExecutingAssembly();
        int count = Application.OpenForms.Count;
        for (int i = 0; i < count; i++)
        {
            Form f = Application.OpenForms[i];
            if (f.GetType().Assembly == currentAssembly && f.Name != "frmMDI") //Here 'frmMDI' is the name of mdiform.
            {
                f.Close();
            }
        }
    }
}

Now call this method in your function

  public void menu2_click(sender,e)
  {
    Helper.HideAllForms();
    Form2 objForm2 =New Form2();
    objForm2.Show();
  }

this works fine for me, hope this will also works for you..

Upvotes: 1

Thomas
Thomas

Reputation: 1321

Create the following function in your MDI form.

public void OpenForm(Form toOpen)
{
    foreach (Form child in MdiChildren)
        child.Close();

    toOpen.MdiParent = this;
    toOpen.Show();
}

Call it in your menu item event handlers as follows OpenForm(new Form1())

Upvotes: 3

Dragon Creature
Dragon Creature

Reputation: 1995

Instead of declaring the windows in the click event make them global. Then what you do is check the Visible property in the form. If it true for an other form then the one you are trying to open you can use the Close() method.

Upvotes: 1

Umesh
Umesh

Reputation: 2732

Show the first form as Modal. The Modal form will not allow to open a new form until you close it.

Upvotes: 0

Brijesh Patel
Brijesh Patel

Reputation: 2958

You need to write code to open that form as below:

For open first form, write code in click event of MDI form,

Dim iobjForm1 As New Form1()
iobjForm1.ShowDialog()

and for secode form,

Dim iobjForm2 As New Form2()
iobjForm2.ShowDialog()

it will open two form at a time.

Hope this will help you.

Upvotes: 0

Related Questions