First off, I'm sorry if this is gonna be some noobish question.
I need advice for managing my multiple windows application forms. I'm programming C#, aided by Visual Studio 2013.

Class Program.cs
This contains the main method, where everything begins, and everything ends.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // This is just a part of my code in the main method. The relevant part I wanna discuss            
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        formLogin objFormLogin = new formLogin();
        Application.Run(objFormLogin); // note this part
    }
}

formLogin
Description: my first form to appear and will be hidden when login is successful; will also reappear when in formMain, the user decides to click the button "Log Out"

private void btnLogin_Click(object sender, EventArgs e)
{
    // Note, this is just a part of the my entire code.
    if (myDataReader.Read())
    {
        MessageBox.Show("Login successful!", "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        txtID.Clear();
        txtPassword.Clear();
        formMain objFormMain = new formMain();
        objFormMain.Show(); // show formMain when login is successful
        this.Hide(); // hide formLogin
    }
}

formMain
Description: second form to appear if user's login is a success

private void btnLogOut_Click(object sender, EventArgs e)
{
   this.Close();
   // I need to unhide the instance of formLogin here
   // I am aware that I need object reference for it (formLogin)
   // But my problem is where to put it so that it is accessible in the entire namespace
}

In the formMain, if the user clicks the log out button, I want formMain to close, and for formLogin to reappear (I've hidden it before, using the this.Hide(); statement.) I can't do so without an object reference. But where do I declare it so that formMain is also "aware" of its existence (in other words, formLogin's object is accessible)? It's only declared in Program's main method. Some unreachable place, I suppose. Therefore, from formMain, I cannot un-hide formLogin.

I can't add it (obj for formLogin) in the Program class (outside of the main method, making it a class variable). Has a lot to do with the [STAThread] thingy.
I've also tried instantiating all form objects in a separate class, only in vain, because some exception shows up at the Application.SetCompatibleTextRenderingDefault(false); part.

Please, don't tell me to just create a new object for formLogin. That'd mean I have to use the .Close() method for it, therefore going back to the main method, and since there is no more code below Application.Run(objFormLogin);, my entire program will eventually terminate. I have to use the .Close method since we do not want another instance running when in fact, it ain't used.

Confusing? Sorry. And thanks.

Nevermind. Kinda worked around it. I don't know if it's the most efficient way, though.

if (myDataReader.Read())
{
    MessageBox.Show("Login successful!", "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    txtID.Clear();
    txtPassword.Clear();
    this.Hide();
    using (formMain objFormMain = new formMain())
    {
        objFormMain.ShowDialog();
    }
    this.Visible = true;
}

My opinion is that you should only maintain a resource as long as you need it. I recommend seeing the tutorial on form to form communication if you have data that is passed between forms. Also, close/dispose of objects you are not actively using. When an application/program makes users computers unresponsive due to poor programming practices the users will likely stop using your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.