C# newbie - "Cannot add a top level control to a control."

M

Mutombo

I was wondering if anyone would be able to help me with this.. I have code
like this...

code ---------------

private void button1_Click(object sender, System.EventArgs e)
{
ClientForm cf = new ClientForm();
this.MainPanel.Controls.Add(cf);

}
}
code ends---------------

...now, this is all contained within MainForm, in short when a user clicks on
the button new Form will be added to the main panel.. that's all fine except
that I keep getting

************** Exception Text **************
System.ArgumentException: Cannot add a top level control to a control.
at System.Windows.Forms.ControlCollection.Add(Control value)
at KungFu.MainForm.button1_Click(Object sender, EventArgs e) in
c:\Documents and Settings\mutombo\My Documents\Borland Studio
Projects\KungFu\MainForm.cs:line 326
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

what does this mean "Cannot add a top level control to a control." and how
do I go about dynamically adding forms?

all help is greatly appreciated..

Mutombo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top