c# - 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();
}
解决方案
你在哪里用的this.StartPosition = FormStartPosition.CenterScreen;
?您应该尝试在表单构造函数中使用。
推荐阅读
- javascript - 如何刷新 node.js HTTP 服务器中的浏览器页面?
- sql - PLS-00382:表达式类型错误。PL/SQL:语句被忽略
- javascript - 在 Spring 中创建条目会引发不受支持的媒体类型
- java - Hibernate Spring Framework Transactional No Transaction In Progress
- python - 从其他脚本访问主函数中的变量
- python - 使用谷歌金融抓取实时股票价格
- reactjs - 如何使用 Next.js 在测试中加载环境变量?
- c# - 如何以编程方式计算图像的(相对)复杂度?
- php - 我正在创建一个 php 脚本来从我计算机上的文件夹中下载视频。视频可以下载,但无法播放。我什至使用文件类型视频/mp4
- r - 更有效地键入列表元素