954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Opening an existing form

In my program, there is a button you click that brings up a new form. This form runs a process. On the form (second form) closing event, I have

e.Cancel = true;
            Hide();


So now the form that runs the process is hidden. Now, the way i want it to work, is that when they click the button to open the form that runs the process *after* its been hidden, it should bring up the existing form instead of a new one. So basically the code for the button that opens form2 will have to check to see that the form is hidden, and if it is, show it. If its not hidden, open a new instance of it. The way i know how to open a new form is

Form2 frm = new Form2();
frm.Show()

But the problem with that is that it opens a new instance every time. How can I show an existing hidden form?

Sorry if I seemed unclear.. let me know. Any help would be greatly appreciated.

Thanks,

SiPex

SiPexTBC
Light Poster
28 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

I *think* I see what you mean. Make the hidde form(from here referred to as formH) a member variable of the form with the open button(from here referred to as formO). That way, only one formH is ever declared and used, and is only destroyed when you telll it to, or close formO.

To do this, put a declaration for formH within the formO class braces, but not within any method blocks. To make the code easier to understand, it's mostly put directly before the formO() constructor.

Now that you have made a single formH available throughout the entire formO, when the open button is pressed, you need to pull it up. This code is pretty much self-explanatory:

//assuming you declared the formH as a variabled named formHInstance....
if (formHInstance == null)
{
    formHInstance == new formH();
}
formHInstance.Show();
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

Thanks worked :)

SiPexTBC
Light Poster
28 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

When you closing any program in c sharp.

this.Hide();

When you closing any program in vb (visual Basic).

Me.close()

hussain_ahmed
Newbie Poster
2 posts since Jan 2012
Reputation Points: 8
Solved Threads: 0
 

Hi,
In my program,Im using a button in a form...when i click on it...it should open an existing form(Login form)...The thing is i knw how to open a new form when i click on button...but i need existing form which is already dr in ma system...im pissed off tryn dis...can anyone help me...

chaitra9
Newbie Poster
1 post since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You