英文:
Cannot hide MetroForm in C#
问题
这是我的代码链接:https://ptero.co/hasunotuwi.cs
我正在创建一个许可证系统,我希望在密钥无效或数据库中找不到密钥时隐藏 MetroForm。这是一个学校项目,所以不必担心数据库凭据以明文存储。在运行代码后,应显示一个消息框,显示"Your activation is not valid.",并隐藏 MetroForm。但实际上,它显示了消息框,MetroForm 也显示出来,好像什么都没有发生。
英文:
So here is my code: https://ptero.co/hasunotuwi.cs
I'm making a license system, I want to hide the MetroForm after the key is invalid or if the key isn't found in the database. It's a school project, so don't worry about the DB credentials being in plain-text. After I run the code It should display a MessageBox saying "Your activation is not valid." and hide the MetroForm. Instead it displays the MessageBox and the MetroForm shows up like nothing happened.
答案1
得分: 0
问题已经解决。问题在于"Load"事件在表单显示之前执行,因此没有要隐藏的内容。相反,我创建了一个名为"isHidden"的布尔变量,并将其赋值为false。接下来,每当我需要隐藏表单时,我将其赋值为true,最后,我创建了一个"Shown"事件的void方法,代码如下:
private void Form1_Shown(object sender, EventArgs e)
{
if(isHidden)
{
Hide();
}
}
英文:
The problem was fixed. The thing is that Load event is executed before the form shows, so there is nothing to hide. Instead I created a bool named isHidden and assigned false to it. Next, I assigned true whenever I needed the form to hide and last I created a void for the Shown event and it looked like this:
private void Form1_Shown(object sender, EventArgs e)
{
if(isHidden)
{
Hide();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论