Q:

Visual Studio C# Controls not working (sometimes)

I'm really confused so I'm hoping someone can help me out here. I'm working on a programming assignment for uni but there's one part that's really been bugging me and I can't move on until it is fixed. I have created two classes. The problems in each are shown here:
class Login : Form1
{
    Form1 f = new Form1();

    public void LoginCorrect()
    {
        Form1.attempts = 3;
        MessageBox.Show("Correct Credentials Entered!");

        f.loginScreenVar = false;
        f.mainScreenVar = true;
        f.ChangeScreen();
    }
 }

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void ChangeScreen()
    {
        //Login Screen
        txtUsername.Visible = loginScreenVar;
        txtPassword.Visible = loginScreenVar;
        btnLogin.Visible = loginScreenVar;
        lblLoginCaption.Visible = loginScreenVar;
        lblUsername.Visible = loginScreenVar;
        lblPassword.Visible = loginScreenVar;
        //Main Screen
        lblWelcomeUser.Visible = mainScreenVar;
        btnViewDetails.Visible = mainScreenVar;
        btnViewAccounts.Visible = mainScreenVar;
        btnLogout.Visible = mainScreenVar;

        MessageBox.Show(loginScreenVar.ToString());
    }
}

I have some controls on screen in my design which consist of text boxes, labels, and buttons, and these are meant to show and hide at diffferent times. I have created some booleans which can be set to true and false which will also set the visibility of these controls to true and false.
My problem is when accessing ChangeScreen() from my Login class, for some reason the controls don't hide when they're meant to. I've literally got a message box in the ChangeScreen() method which outputs the result of 'loginScreenVar' and this is false. Please can someone tell me why my 'Login Screen' controls are NOT hiding even though 'loginScreenVar' = false.
Another thing to note is when calling this code from a button in the Form1 class, it does work. However, due to the brief of my assignment I need to use multiple classes.
I really hope this isn't a bug and someone can help me here because I literally can't move on until this is fixed, thanks!

A:

The issue is that, as noted in the comments, you create a new instance of Form1. 
A whole new object, with own states.
Why can't I see this new instance? - well, if you did f.show() THEN you'd see it.
As it stands, you're still looking at the old instance.
So, what you need is a publicly accessible instance of Form1 which your two classes access, without creating a new instance.
OR
you could also work with two different windows. For example:
Form1_loaded(object sender, EventArgs e)
{
    LoginWindow lw = new LoginWindow();
    var result = lw.ShowDialog();
    if(result == DialogResult.Cancel)
    {
        Application.Quit();
    }
}

Let's assume you have a button for login. When clicked, it checks whether password and user name are correct. If not, the incorrect count gets increased by one. If the incorrect count is >= 3, then you just close the LoginWindow. (Default DialogResult is DialogResult.Cancel). The code might look like this:
LoginBtn_Click(object sender, EventArgs e)
{
    if(UserNameInput.Text == userName && PasswordInput.Text == password)
    {
        failedAttempts = 0;
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
    else
    {
        failedAttempts++;
        if(failedAttempts >= 3)
        {
            MessageBox.Show("Wrong password. Shutting down the application...");
            this.Close();
        }
        else
        {
            MessageBox.Show("Wrong password. " + (3-failedAttempts) + " tries left.");
        }
    }
}

This way, if login isn't successful, app quits. Otherwise the main screen appears.
Note: This is a basic solution. In a more complex app, you'd want more sophisticated output (not hard-coded strings) and comparisions using VariableName.Equals();

