SEVI Posted April 22, 2004 Posted April 22, 2004 Please help! I have a custom dialoge form that is created via code and when I call the form.dispose(), close or even when it goes out of scope my whole main form flickers? I don't get it!! frmProjectLayout ProjectLayoutForm = new frmProjectLayout(this.MainForm); ProjectLayoutForm.ShowDialog(this.MainForm); switch (oProjectLayoutForm.DialogResult) { case DialogResult.OK: break; case DialogResult.Cancel: break; } ProjectLayoutForm.Dispose(); Quote
SEVI Posted May 1, 2004 Author Posted May 1, 2004 Still haven't found an answer to this one. Is it normal for a main form to flicker so much when a form it created goes out of scope? I've tested it out in case it was my project but even a simple example does the same thing.. Have others noticed this or is it just me?? using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 96); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "FORM 2"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Form1"; this.ResumeLayout(false); } #endregion [sTAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { Form2 f2 = new Form2(); f2.ShowDialog(); f2.Close(); } } public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(88, 136); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "HIDE"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { this.Hide(); } } } Quote
*Experts* DiverDan Posted May 1, 2004 *Experts* Posted May 1, 2004 You might try using Form2.ShowDialog() instead. Form2 is disposed of by its on Dispose method without any screen flicker or other annoyances. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
SEVI Posted May 1, 2004 Author Posted May 1, 2004 You might try using Form2.ShowDialog() instead. Form2 is disposed of by its on Dispose method without any screen flicker or other annoyances. Thanks for your response.. Do you mean instead of calling this.Hide() in Form2? I'm keeping the form in scope by calling Hide so I can pass parameters back from Form2 to Form1. Then disposing of it after I'm done.. this is how the text books show it should be done but it looks terrible, especially when theres alot of controls on Form1. Here is the code from my project: frmProjectLayout oProjectLayoutForm = new frmProjectLayout(this.oMainForm); oProjectLayoutForm.ShowDialog(this.oMainForm); switch (oProjectLayoutForm.DialogResult) { // ok was pressed case DialogResult.OK: // populate user selected vars this.oName = oProjectLayoutForm.o_FrameName; this.oFrameSizeF = new SizeF(oProjectLayoutForm.o_FrameSize.Width, oProjectLayoutForm.o_FrameSize.Height); case DialogResult.Cancel: break; Quote
Administrators PlausiblyDamp Posted May 1, 2004 Administrators Posted May 1, 2004 You shouldn't need to call the Form2.Hide() from your button1_click - if the form is a displaye dwith .ShowDialog() then clicking the button will hide the form automatically - but you should still be able to access it's controls / properties from code. Also you may need to modify your switch statement a little.... frmProjectLayout oProjectLayoutForm = new frmProjectLayout(this.oMainForm); DialogResult res = oProjectLayoutForm.ShowDialog(this.oMainForm); switch (res) { // ok was pressed case DialogResult.OK: // populate user selected vars this.oName = oProjectLayoutForm.o_FrameName; this.oFrameSizeF = new SizeF(oProjectLayoutForm.o_FrameSize.Width, oProjectLayoutForm.o_FrameSize.Height); case DialogResult.Cancel: break; } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* DiverDan Posted May 1, 2004 *Experts* Posted May 1, 2004 Well, I'm embarresed...again. You already used the ShowDialog method with Form2 'oProjectLayoutForm.ShowDialog(this.oMainForm);' Therefore, It is not necessary to re-dispose Form2 when it is closed in Form1 (which is why your screen flickers - refreshes). You only need to do this when you open Form2 as 'oProjectLayoutForm.Show(this.oMainForm);' . Also, as you found out, you can pass a reference variable with both methods. Since Form2 is called as ShowDialog, it will Dispose of itself when it is closed in the same way a MessageBox or Save File Dialog are disposed, therefore again...You really cannot Hide Form2. Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
SEVI Posted May 4, 2004 Author Posted May 4, 2004 Oh so simple.. Done! In the end it was kinda simple, but it annoys me that the reference books led me down the wrong path using the Hide() means of creating a custom dialog. They were indicating that by using Close() the form object would be destroyed and therefore data would be inaccessable to the main form. It was totally un-necessary. I was unaware that even when Close() is called on form (that is not the main form of course) the form object and all exposed variables will continue to exist. Therefore as Dan mentioned, no need to Hide the form, just Close it and grab data as required. Lesson learnt.. don't always trust what you read.. Thanks for the help! SEVI Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.