Alright. I've just been told that, instead of creating new forms over and over again using new FormName(); I should simply open 1 form and then hide/show it as I please. The issue now is, that I don't really know where/when I should create these new form instances and how I would reference them. I was thinking that I could create a new instance of all of the forms I need when the program starts and then just hide them until I need them, but I've really got no idea. Can anybody give me any clues on what they would do and how they would do it? Much appreciated.

Good question!
I should create forms as I need them.
Think for instance about a dialog form, informing the user that he has some options he can take, before finishing the application. Would you create such form at the start of the application? I see no need in doing so, it would clutter up your code for one thing.

Yeah. If I create them when I need them it'll be a lot easier I suppose. Here's a question though: How do I reference the form from other classes when it's already been created. Would I need to create a public static variable? If so, how would I create it?

Sorry I don't understand. It's quite hard to explain since I don't know the necessary words to describe what I'm looking for, but let me try again. I want to create a form instance (open it once) and store that instance in a variable so I can access it from anywhere. This'll prevent me from opening duplicate instances which isn't what I want to be doing. I hope that's clearer.

I think I understand exactly what he's asking, as this is something I have used prenty of times (and perfected).

Okay so you know how you declare a new variable? Or a custom object? Maybe decalre a class that you call to (aka it's not static). Same concept for forms. Check this out

First of all, don't create them all at once. Only create them as you need. As you can never guarantee they will all be used (so why waste the time, resources, and cluttered code?)

Okay here's a good example (this is based on a code I am currently working on). I have created a windows form in one of my programs that is designed to display some data that is download. It's seperate from my main form (which is Form1). This new form is called "viewPageSource".

Now I want viewPageSource to show up when I click a button. So I connect a press event to this item. Now when the user presses button (okay it's actually a menu option, but that's not important), the code looks like the following

private void viewSourceToolStripMenuItem_Click (object sender, EventArgs e) //menu option clicked
{
    viewPageSource sourceForm = new viewPageSource(url, postField);
    sourceForm.ShowDialog();

    string downloadedData = sourceForm.downloadedData; //retrieves a string from the form

    sourceForm.Dispose();
}

Now let's take a look at the code in the form to clear this up a little

namespace ScoreTableDetector2_1v7
{
    public partial class viewPageSource : Form
    {
        string url;
        string postField;

        public viewPageSource (string url, string postField)
        {
            InitializeComponent();
            this.url = url;
            this.postField = postField;
        }

        public string downloadedData
        {
            get;
            set;
        }

        /* ... */
    }
}

Okay so there you go. Now here's what we are doing. First of all we assign this form to a variable that we can then use. Notice how I am passing in values, just like you would when initing any other class.

The following line is sourceForm.ShowDialog();. What this code does is doing is display this new form, bringing it up so we can see it. Now the cool part is, ShowDialog will like "pause" the code in the main form, and the code will not progress until after I close the sourceForm form.

Next line, string downloadedData = sourceForm.downloadedData;. In the form, I stored some values to this string, now I am just retrieving them from the sourceForm form. Just like passing back a value you might say.

Then we come to the last line sourceForm.Dispose();, which at least how I understands it, I clean up the resources, letting the garbage collector know this is good to go (or triggering it, I haven't fully understood it, I just know it cleans up resources).

Now I know this isn't the hide/unhide you want to do, but this works rather well. If you do want to use hide thought, simply do something like this

this.Hide();
sourceForm.ShowDialog();
this.Show();

This answer your question? I know it's a lot, hopefully it makes sense, let me know if this is what you were looking for or have more questions

Edited 3 Years Ago by JOSheaIV

This article has been dead for over six months. Start a new discussion instead.