473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling a hidden form

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
6 2408
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
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
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
---------------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
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
How dumb one can be !! :) Thanks alot Dave...a true logical
explanation.

Kudos,
Ahmad

Jul 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
7
by: SamMan | last post by:
I am fairly new to PHP programming, and I'm trying to get my head around this one... I have a form that I am doing validation on via PHP within the same page, so if there is an error, a message...
10
by: Eitan | last post by:
Hello, I want to know, wheter an asp (that call itself in "get" method, has passed any parameter to itself). i.e : in the asp : test_asp.asp : ..... <html> <body>
2
by: Harold | last post by:
I have page A with form field and a button and when button is clicked it opens page B in another window. Page B has a treeview control and that is all. When something is selected I want the page...
1
by: desmcc | last post by:
Hi, I am launching a modal dialog through the usual javascript (window.showmodaldialog). When the modal dialog is complete (ie user selects OK), the calling page then refreshes itself by setting...
0
by: teddysnips | last post by:
I have Search form that allows users to retrieve records into a DataGrid. There are two search criteria - a Month and a Year, which are selected from drop-down lists. There is a server-side...
4
by: Sandman | last post by:
Hi there, So here is the situation I'm in. My PHP script does something like this: 1. Update a DB (only once) 2. Send some post data to another PHP script (only once) 3. Output some stuff...
4
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
4
by: varunbhatia87 | last post by:
I am developing an application in vb.net, in which i m using a treeview control which display nodes generated from database, when i select a node it gives me a form name that comes also from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.