首页 > 解决方案 > Windows 窗体 CenterScreen 属性不起作用

问题描述

我有 2 个表格。首先是登录表单。我将StartPosition属性设置为CenterScreen但是当表单加载时它没有居中。

从这个表格我打开另一个通常居中的表格。当我从第二个表单注销时,登录表单重新打开,现在居中。如何?我需要解释。

尝试过CenterToScreen()this.StartPosition = FormStartPosition.CenterScreen;但没有任何帮助。

编辑:

在按钮上单击第二个表单打开。这是代码。

private void btnLogin_Click(object sender, EventArgs e)
    {
        connection.ConnectionString = "Server = something something";

        connection.Open();
        command.Connection = connection;
        command.CommandText = "SELECT username, password FROM [User] WHERE username='" + textBoxUsername.Text + "' AND password = '" + textBoxPassword.Text + "'";
        sdr = command.ExecuteReader();
        if (sdr.Read())
        {
            userName = textBoxUsername.Text;
            this.Hide();
            SecondForm secondForm = new SecondForm();
            secondForm .ShowDialog();
            this.Close();
        }
        else
        {
            lblWrongUsernameOrPassword.Text = "Wrong username or password!";
            textBoxUsername.Clear();
            textBoxPassword.Clear();
        }
        connection.Close();
    }

标签: c#.netwinforms

解决方案


你在哪里用的this.StartPosition = FormStartPosition.CenterScreen;?您应该尝试在表单构造函数中使用。


推荐阅读