By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Calling a hidden form

P: n/a
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.

Jul 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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();
}

--
Dave Sexton

<ah*********@gmail.comwrote in message news:11**********************@i42g2000cwa.googlegr oups.com...
>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.

Jul 29 '06 #2

P: n/a
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

Jul 29 '06 #3

P: n/a
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.

--
Dave Sexton

<ah*********@gmail.comwrote in message news:11**********************@h48g2000cwc.googlegr oups.com...
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

Jul 29 '06 #4

P: n/a
---------------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(fals e);
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

Jul 31 '06 #5

P: n/a
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

<ah*********@gmail.comwrote in message news:11**********************@m73g2000cwd.googlegr oups.com...
---------------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(fals e);
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

Jul 31 '06 #6

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

Kudos,
Ahmad

Jul 31 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.