Problem with panals

rmokkenstorm

Freshman
Joined
Feb 20, 2006
Messages
31
I have a problem.
On my form (mainform) is use a panal wich should load another form (form1)
now i placed the panal en he should load the form1 when the mainform loads.
but i get this error:

Top-level control cannot be added to a control.

i don't know what to do whit this error.

Code:
public partial class main : Form
    {
        Form1 from1;
        public main()
        {
            InitializeComponent();
        }

        internal Form1 From1
        {
            get
            {
                if (this.from1 == null)
                
                {
                    this.from1 = new Form1();
                                                    
                   this.panel1.Controls.Add(this.from1);                 
                    this.from1.Dock = DockStyle.Fill;
                }

                return this.from1;
            }
        }

        private void main_Load(System.Object sender, System.EventArgs e)
        {
            this.FFrom1();
        }

        
internal void FFrom1()
        {
            this.From1.Visible = true;
        }

this is my code

i use visual C# 2005
and offcourse writing in C#

can anyone help me?
 
The error is being thrown because you cannot load a Form inside a Panel. As the error correctly informs you the Form is a Top Level Control and cannot be added to another controls Control collection. If you wish to load forms inside another form the only way of doing it is to use an MDI form and child forms. Alternatively you will have to lay out the control directly into the panel rather than attempting to load a form into the panel.
 
oke thanks
but i have some sample where he loads his form in this panal.
at least.. i think. gues i'm wrong.

i want to have the situation that i have at the top of my screem some button's, then the pannel or some kind and at the bottom also some button's
this buttons are everywhere the same. so it's kinna useless to put these on every form.
i go search some information about "MDI form and child forms".
thanks
 
My suggestion would be to change your form to a UserControl. A UserControl is very similar to a form, but instead of floating around the desktop it has to be put on another form. This seems to be exactly what you need.
 
Back
Top