How do I make one Form parent of Another.

  • Thread starter Thread starter Devicharan
  • Start date Start date
D

Devicharan

I have two forms, a Main Form and a Child Form.
And In the Main Form, I have a small Async Process, Which
needs to show the child form.

I use mainForm.BeginInvoke(ShowtheNewForm) to
start the async process and in the funciton
(ShowtheNewFOrm) I say ChildForm.Show().

I cannot use ShowDialog here as this blocks the
main form. I want control to be active on the MainForm
also. SO, this requires a Show().

Now the problem is, at time, the Child Form goes
behind the main form. To avoid this, I want to set main
form as Parent to the child form.

As we do in C++ when creating the form, I want to
set the parent. [CreateWindow(Parenthandle)].

Is there a way to do this in C#. When I use
childForm.Parent = mainForm; it gives a Run Time
exception as both of them are top level controls.
 
You can try to set parent using Win32 API:

SetParent(mychildform.Handle, parent.Handle);

which you can use when declared as

[DllImport("user32.dll")]
internal static extern int SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);

Would be interesting to know if this solves the issue with top level
controls error!

HTH
Alex
 
Back
Top