Calling a hidden form

A

ahmad.humyn

I want to call a hidden form. My code goes something like in which the
main calls form1. form1 has a button which creates & calls form2 and
hides itself. Now I have a button in form2 which if pressed should
dispose form2 and then unhide and focus form1.

--------------------------------------------------
static void Main()
{
.....
Application.Run(new Form1());
}
---------------------------------------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
}
----------------------------------------------------
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
// Somehow Form1.Show();
// where Form1 is the hidden form
}
}

Please help out here. I will greatly appreciate it.

Regards.
 
D

Dave Sexton

Hi Ahmad,

Using a TabControl might be a better UI design but if your really want to toggle between two Forms you could just set Form1 as the
Owner of Form2.

{Form1}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2 Owner = this;
form2.Show();
this.Hide();
}

{Form2}
private void button1_Click(object sender, EventArgs e)
{
Owner.Show();
this.Dispose();
}
 
A

ahmad.humyn

Dear Dave, I tried what you suggested and it seemed completely logical
to me what you told. :
I set this in my form1 code -> frm.Owner = this; and then I tried doing
this in my form2 code Owner.Show(). But Visual Studio gives a runtime
error on Owner.Show saying that "NullReferenceException was Unhandled
:: Object reference not set to an instance of an object."

What should I do? Greatly appreciate your help

Regards
 
D

Dave Sexton

Hi Ahmad,

Sounds like Owner is null. Are you sure you set the correct Form's Owner property? The code I posted works. Post your code and
I'll take a look.
 
A

ahmad.humyn

---------------Program.cs-------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
-------------------------------------------------------
------------Form1.designer.cs----------------
namespace WindowsApplication3
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(117, 39);
this.button1.TabIndex = 0;
this.button1.Text = "Call form2 and HIDE";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(35, 66);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(117, 38);
this.button2.TabIndex = 1;
this.button2.Text = "Call form2 and DISPOSE";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(35, 119);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(117, 37);
this.button3.TabIndex = 2;
this.button3.Text = "Exit App";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new
System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(184, 175);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
----------------------------------------------------------
--------------------Form1.cs------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Dispose();
}

private void button3_Click(object sender, EventArgs e)
{
this.Close();
//Application.Exit();
}

}
}
-------------------------------------------------------
------------Form2.designer.cs----------------
namespace WindowsApplication3
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(42, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(107, 34);
this.button1.TabIndex = 0;
this.button1.Text = "Call Form1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(42, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(107, 35);
this.button2.TabIndex = 1;
this.button2.Text = "Exit App";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(189, 116);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
----------------------------------------------------------
--------------------Form2.cs------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
Owner.Show();
}

private void button2_Click(object sender, EventArgs e)
{
// MessageBox.Show(Application.AllowQuit.ToString());
Application.Exit();
}
}
}

---------------------------------------------------------
=============================

Thanks for helping out Dave. Really new to this framework and have
small problems like these!

Regards
 
D

Dave Sexton

Hi Ahmad,

This is a snippet from your code (Form2.cs):
private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
Owner.Show();
}

Notice that you called Dispose on the Form before you tried to access its Owner property (which is actually the same as this.Owner).
Once you dispose of an object, in this case the Form, you shouldn't try to access any of its members. Reversing the order in which
you call these two methods should do the trick:

// Access this.Owner property and show the Form
this.Owner.Show();

// Dispose of this Form.
this.Dispose();

--
Dave Sexton

---------------Program.cs-------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
-------------------------------------------------------
------------Form1.designer.cs----------------
namespace WindowsApplication3
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(117, 39);
this.button1.TabIndex = 0;
this.button1.Text = "Call form2 and HIDE";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(35, 66);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(117, 38);
this.button2.TabIndex = 1;
this.button2.Text = "Call form2 and DISPOSE";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(35, 119);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(117, 37);
this.button3.TabIndex = 2;
this.button3.Text = "Exit App";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new
System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(184, 175);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
----------------------------------------------------------
--------------------Form1.cs------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Dispose();
}

private void button3_Click(object sender, EventArgs e)
{
this.Close();
//Application.Exit();
}

}
}
-------------------------------------------------------
------------Form2.designer.cs----------------
namespace WindowsApplication3
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(42, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(107, 34);
this.button1.TabIndex = 0;
this.button1.Text = "Call Form1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(42, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(107, 35);
this.button2.TabIndex = 1;
this.button2.Text = "Exit App";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(189, 116);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
----------------------------------------------------------
--------------------Form2.cs------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
Owner.Show();
}

private void button2_Click(object sender, EventArgs e)
{
// MessageBox.Show(Application.AllowQuit.ToString());
Application.Exit();
}
}
}

---------------------------------------------------------
=============================

Thanks for helping out Dave. Really new to this framework and have
small problems like these!

Regards
 
A

ahmad.humyn

How dumb one can be !! :) Thanks alot Dave...a true logical
explanation.

Kudos,
Ahmad
 

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