Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have inherited Form1 to Form2. I want to show Form2 as MDI child. But, while showing it throws "Error creating window handle.".

C#
Form1: Form
{
}

Form2: Form1
{
}

MdiForm: Form
{
     Form2 objForm = new Form2();
     objForm.MdiParent = this;
     objForm.WindowState = FormWindowState.Maximized;
     objForm.ControlBox = false;
     objForm.ShowIcon = false;
     objForm.Show();
}


Here, Form2 object is working fine as a individual form but its not working if i make it child form of MDI. I did not get any error log.

Please help to resolve it. Thanks in advance.
Posted
Updated 30-Oct-14 3:00am
v2
Comments
BillWoodruff 30-Oct-14 9:28am    
First: where are you executing the code shown inside 'MdiForm ? All of these must be executed inside a Method, or an EventHandler, like a Form Load EventHandler:

objForm.MdiParent = this;
objForm.WindowState = FormWindowState.Maximized;
objForm.ControlBox = false;
objForm.ShowIcon = false;
objForm.Show();

This is probably an unnecessary question: but have you created both Form1 and Form2 in design-mode, and they are completely defined Forms ? Did you create Form2 in design-mode, and then go in and modify its class definition so it reads:

public partial class Form2 : Form1

If both Form1, and Form2, are standard Forms, this code should work: what version of .NET are you using ? Put a break-point on the line: objForm.MdiParent = this; and single-step through the code (F11 in Visual Studio), then tell us where the error occurs.

Note that because Form2 inherits from Form1, you can disable the 'ControlBox in Form2, but you can't hide it if Form1's 'ControlBox is set to 'true.

1 solution

From that little, we can;t really answer your problem.
But there are a couple of things you can check.
First, make sure that the MdiForm instance has the IsMDIContainer property set to true.
Then, check exactly where you are executing the code you show above: if the MdiForm instance has not been shown yet, then it won't have a handle, so the MDI chiold will have problems displaying.
Try moving the code to the MdiForm.Shown event and see it that fixes it - the parent should have a handle by that stage.
 
Share this answer
 
Comments
BillWoodruff 30-Oct-14 9:59am    
I've asked the OP some detailed questions. If they are executing

objForm.MdiParent = this;

In Form scope, outside a Method or EventHandler, then they'd get an invalid token error on compile, rather than can't create handle error: in VS 2013, they would, anyway.
PrasadKumar 24-Nov-14 3:12am    
Thanks for replies.

It solved. My logic was wrong. I was trying to use object of MDIForm of DLL into project.

What I did,
a. Created a class library (DLL) and added Form1 (which is inherited by System.Windows.Form class) and MDIForm.
b. Created new project and used above DLL.
c. In this project , added Form2 (which is inherited by Form1 of DLL),
d. In Program.cs, Main() method created object of MDIForm (of DLL)
e. Tried to make MDI parent of Form2 object to MDIForm object. (it was wrong logic).
f. Now, I have created MDIFrom in project (and removed from DLL) and its working fine.

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900