Create Form in advance

G

Guest

I want create form in advance because of my device CPU speed.When I need it,I
just show it.But I encounter a problem.In Form1,I create Form2 in constructor
Form2 frm2 = new Form2();
then
private void button1_Click(object sender, System.EventArgs e)
{
frm2.ShowDialog();
}

If I design Form2 this way:
private void Form2_Load(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
....

}

The third time I press button1,I'll got a ObjectDisposedException.
If I omit this.WindowState = FormWindowState.Maximized; Everything is OK

Anyone knows why?

Thx,
wyghf
 
M

ManniAT

Hi wyghf,

an easy way to do what you want is to handle the visibility state of the "on demand forms".
So you create them, show them and set their visibility to false.
The first Show takes time - so with this approach you have really done all what is time consumming.
In the onload of the form set visibility to false.

Afterwards - when you need the form just make it visible and bring it to foreground.
If you want to handle it like a dialog - save the information that it is visible in your main form.
When the main form is activated - pass the focus to the "dialog".

And instead of closing the "dialog" just signal the mainform that input is done - and hide it again.

HTH

Manfred
 
G

Guest

Thanks for your reply.I've solved this problem before: I use Show() , set
e.Cancel = false and Visible = false in Closing event handler.But I just want
to know why we cannot use
this.WindowState = FormWindowState.Maximized; If use ShowDialog(),my
solution cannot work.
 
M

ManniAT

Hi,

PPC does not have the concept of minized windows.
Such things (mini-box) result in a change of the Z-Order of the windows.

And of course - even under windows ShowDialog would not work.
A dialog is a child window of some parent - and it will gain the focus until it is gone.
Try it on the desktop - create a form (you can even minimize it there) as a dialog and see whats going on.

Of course you can use a modless dialog - and that is what you do with Show of the form.

HTH

Manfred
 
G

Guest

Maybe I cannot describe my purpose clearly.I know the difference between
modal and modeless dialog.I paste my simplyfied test code here,so you can
understand what I mean(I use vs2003 and Windows CE.NET emulator).You should
pay attention to myForm1_Load(...).

form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace panelscroll
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button2;

public Form1()
{

InitializeComponent();


}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 112);
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(266, 255);
this.Controls.Add(this.button2);
this.Text = "Form1";

}


static void Main()
{
Application.Run(new Form1());
}

myForm1 frm = new myForm1();

private void button2_Click(object sender, System.EventArgs e)
{
//myForm1 frm = new myForm1();
//try
//{
frm.ShowDialog();
//}
//finally
//{
// frm.Dispose();
//}

}


}
}

form2.cs
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace panelscroll
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class myForm1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.Button button35;
private System.Windows.Forms.TextBox textBox35;
private System.Windows.Forms.Label label35;
private System.Windows.Forms.TabPage tabPage5;
private System.Windows.Forms.MainMenu mainMenu1;

public myForm1()
{
uint et;

et = GetTickCount();

InitializeComponent();

et = GetTickCount() - et;

MessageBox.Show("Load Time: " + et.ToString() + "ms");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.button35 = new System.Windows.Forms.Button();
this.textBox35 = new System.Windows.Forms.TextBox();
this.label35 = new System.Windows.Forms.Label();
this.tabPage5 = new System.Windows.Forms.TabPage();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage5);
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(240, 268);
//
// button35
//
this.button35.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.button35.Location = new System.Drawing.Point(160, 13);
this.button35.Size = new System.Drawing.Size(64, 20);
this.button35.Text = "button35";
//
// textBox35
//
this.textBox35.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.textBox35.Location = new System.Drawing.Point(76, 13);
this.textBox35.Size = new System.Drawing.Size(80, 22);
this.textBox35.Text = "textBox35";
//
// label35
//
this.label35.Font = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular);
this.label35.Location = new System.Drawing.Point(8, 17);
this.label35.Size = new System.Drawing.Size(64, 20);
this.label35.Text = "label35";
//
// tabPage5
//
this.tabPage5.Controls.Add(this.button35);
this.tabPage5.Controls.Add(this.textBox35);
this.tabPage5.Controls.Add(this.label35);
this.tabPage5.Location = new System.Drawing.Point(4, 21);
this.tabPage5.Size = new System.Drawing.Size(232, 243);
this.tabPage5.Text = "5";
//
// myForm1
//
this.ClientSize = new System.Drawing.Size(298, 418);
this.Controls.Add(this.tabControl1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "Form1";
this.Load += new System.EventHandler(this.myForm1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

[DllImport("coredll.dll", EntryPoint="GetTickCount")]
public static extern uint GetTickCount();

private void myForm1_Load(object sender, System.EventArgs e)
{
/* if you remove the following comment,if you press mainform's
button2,then click X to close myform,repeat these steps
* you'll get an exception */
//this.WindowState = FormWindowState.Maximized;
}
}
}
 
M

ManniAT

OK,

now I understand your problem!
I assume - if you explicit maximize your mainform the "ShowDialog" does something different.
I'm not 100% shure but in native WinAPI "EndDialog" destroys the underlying window.
And ShowDialog - followed by the clicking the X does IMHO something like "EndDialog" - wich results in a dispose.

But anyhow - setting form1 to maximized should not interfear with that.

So in either cases - no matter if a or b is correct:
a.) ShowDialog and click X on it - should dispose
b.) ShowDialog and click X on it - should NOT dispose

WindowState of form1 should not change this behavior - so either it should allway dispose - or never.

I think you can prove the disposement by simply add a breakpoint to the dispose of form2.
And maybe you can find (via stacktrace) what makes the call.

Sorry for the confusion - but I assumed a.) - and therefore allways used Show instead of ShowDialog for
"preloaded forms".

Manfred
 
G

Guest

Thanks.I set breakpoint as you told me.It's very strange that whether I set
WindowState to Maximized or not,it'll will not call dispose.Perhaps you can
paste my test code to vs2003,and test
by yourself.

Why I set to Maximized,because I want to have a nonresizable and
nonmovable window which must have a X icon.I guess if setting
WindowState,some behaviors are changed.If I don't set WindowState
to Maximized,ShowDialog() works very well.
 

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