Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
there are 2 forms Main and Main1.Main is a MdiContainer.bpaper is also a form. here it shows the form bpaper but it also gives an exception on line one.Show(); of the form Main.
saying "error creating window handle"

purpose of this code is to run the form Main in the background,while the Main1 is running on top of it.
once the Main1 is loaded it has to check the system time and if that value is less than 4.00p.m it should prompt the bpaper from
if the time value is grater than 4.0 p.m then the application should exit.

in Main form load:

C#
private void Main_Load(object sender, EventArgs e)
{
  Main1 one = new Main1();
  one.MdiParent = Main.ActiveForm;
  one.Show();
}




in Main1 form load:

C#
private void Main1_Load(object sender, EventArgs e)
{
  t1 = DateTime.Now;
  t2 = Convert.ToDateTime("16:00:00 ");
  bpaper test = new bpaper();

//allow the bpaper to be appear (time before 4oclock)       
   if (t1 < t2)                                 
   {
     MessageBox.Show("less than 4.00 P.M");
     this.Hide();
     test.Show();
   }
//do not allow the bpaper to be appear after 4oclock
   else                                              
   {     
     MessageBox.Show("Time out ", t1.ToString());
     Application.Exit();
   }
}


please help me to find out what is wrong with this code.
Posted
Comments
Bala Selvanayagam 3-Oct-11 4:08am    
can you try
one.MdiParent =this;
instead of
one.MdiParent = Main.ActiveForm;
Bala Selvanayagam 3-Oct-11 5:00am    
May be a memory leak ? - calling the forms in a loop ?

let me get your environment right
1. form "Main" is an MDIParent ?
2. form Main1 is a normal form ?
3. What is this bpaper ? and does not look a normal form and is this inherited from some other forms ?
Member 7779792 3-Oct-11 5:21am    
1. form "Main" is an MDIParent ? Main is the parent of Main1
2. form Main1 is a normal form ? yes
3. bpaper is also a normal form

You don't need to get the ActiveForm - that may even by your problem. Instead, use the current instance:
C#
private void Main_Load(object sender, EventArgs e)
{
  Main1 one = new Main1();
  one.MdiParent = this;
  one.Show();
}




"still the exception is occurring at the same place!"

Then the problem is that you are using Hide in the Load event - this is getting execturted before the MDI parent is completely ready for it: move your code to the form Shown event instead:
C#
private void Main1_Shown(object sender, EventArgs e)
{
  t1 = DateTime.Now;
  t2 = Convert.ToDateTime("16:00:00 ");
  bpaper test = new bpaper();

//allow the bpaper to be appear (time before 4oclock)
   if (t1 < t2)
   {
     MessageBox.Show("less than 4.00 P.M");
     this.Hide();
     test.Show();
   }
//do not allow the bpaper to be appear after 4oclock
   else
   {
     MessageBox.Show("Time out ", t1.ToString());
     Application.Exit();
   }
}


But why are you doing it that way anyway - bpaper is not a MDI child, so why create the Main1 instance at all? Why not do this in the Main form instead, or even in the Main method of the program.cs file?
 
Share this answer
 
v2
Comments
Member 7779792 3-Oct-11 4:41am    
still the exception is occurring at the same place!
OriginalGriff 3-Oct-11 5:08am    
Answer updated
Member 7779792 3-Oct-11 5:30am    
from where is this function {Main1_Shown()} is being called? is it by one.Show(); ?

you said that this same thing can be done in the Main.can you please explain it?
OriginalGriff 3-Oct-11 5:38am    
Read what I said: "move your code to the form Shown event instead" - look in the events list for your Main1 form in the VS designer.
The Main method is called when your application starts, and it displays the Main form for you - it is located in the program.cs file. If you check the time there, you can display your message and close the application before the Main form is even created! Your main form can then be your bpaper one, if that is what you need.
BillWoodruff 3-Oct-11 9:59am    
+5 I do think his problem was only with the invalid setting of the MDIParent property of his instance named 'one' of the Main1 Form. That's definitely a breaking error right there.
You need to modify your Main form IsMdiContainer properties change True and one.MdiParent =this;

Hope Be helpful,
Theingi Win
 
Share this answer
 

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