Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have form open and i am going to another form creating data and again passing to the already open form...So rt now mulitple forms are coming,....How to avoid..Is it possible to pass data without show dialog in windows application c#....
Posted
Comments
CHill60 5-Apr-13 10:14am    
It's not clear what your problem is ... in what way is the other form "creating data" - if it's not capturing data from a user then why show it?
Richard MacCutchan 5-Apr-13 10:15am    
Change the visible property of the original form from True to False, and vice versa to get it back.
Sergey Alexandrovich Kryukov 5-Apr-13 10:24am    
It really depends.
—SA

There are only two ways to display a form in .NET: Show and ShowDialog.
If you have multiple copies appearing then it is not the use of either that is the problem - it is that you are constructing new instances instead of using the same instance each time.

Create a private class level variable to hold your form instance:
C#
private MyForm myForm;
Do not assign it a value.
When you want to show or update the data, check the value:
C#
if (myForm == null)
    {
    myForm = new MyForm();
    myForm.FormClosing += new FormClosingEventHandler(myForm_FormClosing);
    }
...pass the data to your form properties
myForm.Show();
And clear the variable when the form is closed:
C#
void myForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    ...collect your data from the form properties if necessary
    myForm = null;
    }
 
Share this answer
 
Comments
Korathu 2 5-Apr-13 10:57am    
can u make it more clear...
OriginalGriff 5-Apr-13 11:03am    
What part doesn't make sense? It's difficult to make things clearer when I don't know what you don't understand! :laugh:
It would be the best to avoid using multiple forms at all. You can have just one main form and never create more forms (I don't count few modal forms, used like dialogs). What you have as forms now could be turned panels or tab pages (using TabControl. This way, transitions from one "form" to another would be just hiding one panel and showing another one. With tab pages, it's even simpler: the user select the tabs needed at the moment, one at a time. Think about changing of your design this way; it will cover many cases.

A more advanced variant of the same thing would be the dockable UI, pretty much like in Visual Studio.

—SA
 
Share this answer
 
This works likea charm for me:
C#
private void OpenAForm_Click(object sender, EventArgs e)
        {
            bool found = false;
            try
            {
                for (int i=0;i<Application.OpenForms.Count;i++)
                {
                    //Checks if the window is already open, and brings it to the front if it is
                    Form n = Application.OpenForms[i];
                    if (n.Name == "formName")
                    {
                        n.BringToFront();
                        found = true;
                    }
                }
                if (!found)
                {
                    YourForm aForm = new YourForm ();
                    aForm.Name = "formName";
                    aForm.Show();
                }
            }
            catch
            {
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900