Visual Studio C# 控件不工作(有时)

Visual Studio C# Controls not working (sometimes)

我真的很困惑所以我希望有人能帮助我。我正在为 uni 做一项编程任务,但有一个部分一直困扰着我,直到它被修复我才能继续前进。我创建了两个 classes。每个问题都显示在这里:

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());
    }
}

我的设计中有一些屏幕控件,包括文本框、标签和按钮,它们用于在不同时间显示和隐藏。我创建了一些可以设置为 true 和 false 的布尔值,它们还将这些控件的可见性设置为 true 和 false。

我的问题是当从我的登录 class 访问 ChangeScreen() 时,出于某种原因,控件没有按预期隐藏。我确实在 ChangeScreen() 方法中得到了一个消息框,它输出 'loginScreenVar' 的结果,这是错误的。请有人告诉我为什么我的 'Login Screen' 控件没有隐藏,即使 'loginScreenVar' = false.

另一件需要注意的事情是,当从 Form1 class 中的按钮调用此代码时,它确实有效。但是,由于我的任务很简单,我需要使用多个 classes.

我真的希望这不是一个错误并且有人可以在这里帮助我,因为在修复之前我真的无法继续前进,谢谢!

问题是,如评论中所述,您创建了 Form1 的新实例。

一个全新的对象,有自己的状态。

Why can't I see this new instance? - 好吧,如果你做了 f.show() 那么你就会看到它。

就目前而言,您仍在查看旧实例。

因此,您需要一个可公开访问的 Form1 实例,您的两个 类 无需创建新实例即可访问该实例。

您还可以使用两个不同的 windows。例如:

Form1_loaded(object sender, EventArgs e)
{
    LoginWindow lw = new LoginWindow();
    var result = lw.ShowDialog();
    if(result == DialogResult.Cancel)
    {
        Application.Quit();
    }
}

假设您有一个登录按钮。单击时,它会检查密码和用户名是否正确。如果不是,则不正确的计数会增加 1。如果不正确的计数 >= 3,那么您只需关闭登录窗口。 (默认 DialogResult 是 DialogResult.Cancel)。代码可能如下所示:

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.");
        }
    }
}

这样,如果登录不成功,应用就会退出。否则出现主屏幕。

注意:这是一个基本的解决方案。在更复杂的应用程序中,您需要更复杂的输出(不是硬编码字符串)和使用 VariableName.Equals();

的比较

在 winforms 中更改屏幕的更好方法是创建两个单独的面板,每个面板都包含要显示和隐藏的所需控件,以便您可以在它们之间切换

代码示例:

Form1_loaded(object sender, EventArgs e)
{
    LogInPanel.Visible=true;
}
private void ConnectBtn_Click(object sender, EventArgs e)
{
    // Do your checking here
   //        IF conditions met
   MainPanel.Visible=true;
}
private void DisconnectBtn_Click(object sender, EventArgs e)
{
    // Do your checking here
   //        IF conditions met
   LogInPanel.Visible=true;
}

如果您想保持方法论,请确保您的 program.cs 运行登录 class 而不是 Form1 class

现在让我们保持简单(并按照您开始的风格):

public partial class Form1 : Form //Change the default "form1" "Button1" etc names as soon as possible
{
    private bool loginScreenVar = true; //when naming booleans, use "truth test" sounding names like isLoginScreenMode
    private bool mainScreenVar = true;

    public Form1() //this is a constructor, a method that is always called when a new instance of this object is created
    {

        InitializeComponent();

        //use the constructor to set things up
        loginScreenVar = true;
        mainScreenVar = false;
        ChangeScreen();//make sure loginscreen is showing
    }

    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());
    }

    //call this method when the login is correct
    public void LoginCorrect()
    {

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

    //double click your login button in the forms designer to add this click event handler
    public void LoginButton_Clicked(object sender, ClickEventArgs e){
        if(txtUsername.Text == "user" && txtPassword.Text == "pass"){
            LoginCorrect();
        } else {
            MessageBox.Show("Login incorrect");
        }
    }
}

忘记 class Login:Form 东西,除非你真的想探索对象实例化并为事物制作你自己的 classes。当您的应用程序启动时,您的 Form1 将显示出来,执行其中的所有逻辑