473,761 Members | 4,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
{
InitializeCompo nent();
}

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

private void button1_Click(o bject 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 2433
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(o bject sender, EventArgs e)
{
Form2 form2 = new Form2();
form2 Owner = this;
form2.Show();
this.Hide();
}

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

--
Dave Sexton

<ah*********@gm ail.comwrote in message news:11******** **************@ i42g2000cwa.goo glegroups.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()
{
InitializeCompo nent();
}

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

private void button1_Click(o bject 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 "NullReferenceE xception 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*********@gm ail.comwrote in message news:11******** **************@ h48g2000cwc.goo glegroups.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 "NullReferenceE xception 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.Collecti ons.Generic;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
Application.Run (new Form1());
}
}
}
-------------------------------------------------------
------------Form1.designer. cs----------------
namespace WindowsApplicat ion3
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.button3 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(35, 15);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(117, 39);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Call form2 and HIDE";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(35, 66);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(117, 38);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "Call form2 and DISPOSE";
this.button2.Us eVisualStyleBac kColor = true;
//
// button3
//
this.button3.Lo cation = new System.Drawing. Point(35, 119);
this.button3.Na me = "button3";
this.button3.Si ze = new System.Drawing. Size(117, 37);
this.button3.Ta bIndex = 2;
this.button3.Te xt = "Exit App";
this.button3.Us eVisualStyleBac kColor = true;
this.button3.Cl ick += new
System.EventHan dler(this.butto n3_Click);
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
13F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(184, 175);
this.Controls.A dd(this.button3 );
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

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

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

private void button3_Click(o bject sender, EventArgs e)
{
this.Close();
//Application.Exi t();
}

}
}
-------------------------------------------------------
------------Form2.designer. cs----------------
namespace WindowsApplicat ion3
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(42, 12);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(107, 34);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Call Form1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(42, 64);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(107, 35);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "Exit App";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new
System.EventHan dler(this.butto n2_Click);
//
// Form2
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
13F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(189, 116);
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
}
}
----------------------------------------------------------
--------------------Form2.cs------------------------
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeCompo nent();
}

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

private void button2_Click(o bject sender, EventArgs e)
{
// MessageBox.Show (Application.Al lowQuit.ToStrin g());
Application.Exi t();
}
}
}

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

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(o bject 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*********@gm ail.comwrote in message news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
---------------Program.cs-------------------------
using System;
using System.Collecti ons.Generic;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
Application.Run (new Form1());
}
}
}
-------------------------------------------------------
------------Form1.designer. cs----------------
namespace WindowsApplicat ion3
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.button3 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(35, 15);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(117, 39);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Call form2 and HIDE";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(35, 66);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(117, 38);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "Call form2 and DISPOSE";
this.button2.Us eVisualStyleBac kColor = true;
//
// button3
//
this.button3.Lo cation = new System.Drawing. Point(35, 119);
this.button3.Na me = "button3";
this.button3.Si ze = new System.Drawing. Size(117, 37);
this.button3.Ta bIndex = 2;
this.button3.Te xt = "Exit App";
this.button3.Us eVisualStyleBac kColor = true;
this.button3.Cl ick += new
System.EventHan dler(this.butto n3_Click);
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
13F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(184, 175);
this.Controls.A dd(this.button3 );
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

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

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

private void button3_Click(o bject sender, EventArgs e)
{
this.Close();
//Application.Exi t();
}

}
}
-------------------------------------------------------
------------Form2.designer. cs----------------
namespace WindowsApplicat ion3
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(42, 12);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(107, 34);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Call Form1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(42, 64);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(107, 35);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "Exit App";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new
System.EventHan dler(this.butto n2_Click);
//
// Form2
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
13F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(189, 116);
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
}
}
----------------------------------------------------------
--------------------Form2.cs------------------------
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeCompo nent();
}

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

private void button2_Click(o bject sender, EventArgs e)
{
// MessageBox.Show (Application.Al lowQuit.ToStrin g());
Application.Exi t();
}
}
}

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

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
7527
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 "Place_Selected" with the value from the select (naam_keuze.value) in the function the value becomes the $zoek_id and searches in the database for the record with the id of $zoek_id
7
2553
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 will show to the user above the form. If all goes well, I would like to call a processing script that I have already set up and working to format the users response and send it out to a specified e-mail... I'll call it processing.php Is there a...
10
1602
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
2210
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 B item to pouplate a form field on page A. Can this be done and how?
1
1945
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 the location.href of the window to be itself (ie the modal dialog changes a label on the calling page ). I can see that this causes the page load code of the calling page to execute in the code behind page. I can also see clearly that this updates...
0
2389
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 button called "cmdFilter" which retrieves data from the database using any criteria thus selected. For any of the records retrieved into the grid match certain conditions, one of the columns will be formed into a hyperlink. Clicking on this will...
4
1955
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 related to 1 and 2. I've done #2 by using input type="hidden" on a form, like so: print "<form action='../dir/register.php' method='post'
4
3588
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 order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In the corresponding input field (text box) the user enters a new category order which gets stored in the...
4
4524
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 datahase, now i have to call this form. how it should be?? Can any one help.
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.