open multiple forms in PANEL .. C SHARP .net

sumair_coolboy

Newcomer
Joined
Sep 9, 2011
Messages
3
I have 4 forms..

MAINFORM..

FORM1
FORM2
FORM3


In my mainForm .. I have 3 button and 1 panel...

I want to create application in which..

If i click button1.. PANEL will display form1..

if i click button2 .. panel will display form2..

if i click button3 .. panel will display form3...
 
Forms aren't normally contained inside of another form in that way, if you were to make form1, form2 and form3 into user controls instead then things would be a lot easier.
 
Maybe try this

Probably being the Laziest programmer in the known universe...

When I need to do something like this in VS10

  • I design a from in the designer
  • I change the inherited item from form to panel
  • I modify InitializeComponent() in the designer code, to start as a panel instead of a form

Now just add the new panels to your form and make them visible and invisible as you like.

This works quite well, Quick and dirty :)
 
Re: Maybe try this

Not sure about lazy as it seems to be effort than just using a UserControl to get the same result but without having to hack at designer generated code.
 
Of course, you are right... as usual :D

Can't remember why I started doing something stupid like this in the first place.

I only recently started programming again, after a break of a year or so... Must still be rusty
 
I found the solution.. .
Code:
  private void MainForm_Load(object sender, EventArgs e)
        {
            this.btn_show1.Click += new EventHandler(btn_show_Click);
            this.btn_show2.Click += new EventHandler(btn_show_Click);
            this.btn_show3.Click += new EventHandler(btn_show_Click);
        }

        void btn_show_Click(object sender, EventArgs e)
        {
            this.pnl_ShowForms.Controls.Clear();
            int tag = Convert.ToInt32( (sender as Button).Tag);
            switch (tag)
            {
                case 1:
                    Form1 frm1 = new Form1();
                    frm1.FormBorderStyle = FormBorderStyle.None;
                    frm1.Dock = DockStyle.Fill;
                    frm1.WindowState = FormWindowState.Maximized;
                    frm1.TopLevel = false;
                    this.pnl_ShowForms.Controls.Add(frm1);
                    frm1.Show();
                    break;
                case 2:
                    Form2 frm2 = new Form2();
                    frm2.FormBorderStyle = FormBorderStyle.None;
                    frm2.Dock = DockStyle.Fill;
                    frm2.WindowState = FormWindowState.Maximized;
                    frm2.TopLevel = false;
                    this.pnl_ShowForms.Controls.Add(frm2);
                    frm2.Show();
                    break;
                case 3:
                    Form3 frm3 = new Form3();
                    frm3.FormBorderStyle = FormBorderStyle.None;
                    frm3.Dock = DockStyle.Fill;
                    frm3.WindowState = FormWindowState.Maximized;
                    frm3.TopLevel = false;
                    this.pnl_ShowForms.Controls.Add(frm3);
                    frm3.Show();
                    break;
            }
        }
 
Last edited by a moderator:
If you're going to be using these exactly like controls, why would you create them as forms? It's no extra effort to use a UserControl instead. All you are doing is opening yourself up to the possibility of quirky or problematic unexpected behavior.
 
An alternative: you could set your application's main form to be a MDI container, where your main form houses all other MDI child forms. Pretty much like a traditional image processor (Photo Shop) if you don't know what I'm talking about. By using this you could easily open / close and set the position of all windows through the buttons you described.

http://msdn.microsoft.com/en-us/library/d4dabts7(v=vs.80).aspx
 
Back
Top