如何在C#中关闭或隐藏表单? [英] How do I close or hide a form in C# ?

查看:73
本文介绍了如何在C#中关闭或隐藏表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表格。第一个Form1,包含一个名为Login的按钮。在单击表单1中的按钮后,登录就是表单。现在,当我单击按钮确定时,我希望关闭Form1和Login并直接转到最后一个Form,即MainPage。但是当我尝试它时它没有用。这是代码:





I have 3 forms. First Form1 that contains a button named Login. And the Login is the form after you clicked the button in form 1. Now, when I clicked the button Ok, I want the Form1 and Login to close and go directly to the last Form which is MainPage. But when i try it it didn't work. Here's the code:


private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=H:\WindowsFormsApplication1\WindowsFormsApplication1\AmericanGirls.mdf;Integrated Security=True;User Instance=True");
            SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From Table1 where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                MessageBox.Show("Welcome");
                this.Hide();
                Form1 se= new Form1();
                se.Close();
                MainPage ss = new MainPage();
                ss.Show();
 
            }
            else
            {
                MessageBox.Show("Merlin's Beard! Are You Kidding Me? -_-");
            }
        }





我可能忘了要放东西或者我的代码真的错了。我还是Visual Studio和C#的新手,所以我真的不知道该怎么做。请帮助



我尝试过的事情:



我试过了代码Form.close和Hide也是visibilty。但它仍然没有用。



I may have forgot something to put or my code is just really wrong. I'm still new to Visual Studio and C# so I really don't know what to do. Please Help

What I have tried:

I have tried the codes Form.close and Hide also the visibilty . But it still didnt work.

推荐答案

问题在于:

The problem is here:
Form1 se= new Form1();
se.Close();



这会关闭表单的新实例,这与你能看到的那个不一样 - 你需要得到正确的实例。 />
这很复杂,因为我假设您显示的代码位于Login表单中,并且根本不知道Form1实例。 正确的方法是让登录表单告诉Form1它已经登录并且它应该关闭 - 这实际上并不困难,但具体取决于你用什么来显示登录表单中的第一个地方。



1)如果你使用了ShowDialog,那么很简单:当你按下按钮时,在登录表单上调用Close,代码将继续在Form1之后ShowDialog电话。 Form1然后可以隐藏或关闭它自己并显示MainPage。



2)如果你使用Show那么它也很简单 - 它听起来很复杂。您需要做的就是在您的登录表单中创建一个事件,该事件在Form1中处理,并执行上述(1)中所述的所有操作。这很容易做到:在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]会告诉你如何。



可能你想隐藏Form1,而不是关闭它 - 因为如果Form1是启动表单,那么关闭它会关闭应用程序所以你永远不会看到MainPage!但如果你这样做,那么你需要处理MainPage.FormClosed事件并关闭Form1或者应用程序将永远不会退出(但用户将永远无法看到它!)



为自己做几个好处:

1)永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

2)绝不以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]

3)停止使用Visual Studio默认名称 - 你可能还记得今天的TextBox8是手机号码,但是你必须在三个版本中修改它几周时间,你会吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......


That closes the new instance of the form, which is not the same as the one you can see - you need to get the right one.
That's complicated, because I assume that the code you show is in the Login form, and it doesn't know about the Form1 instance at all. The "proper" way to do this is to have the login form tell Form1 that it has logged in and it should close - which isn't actually difficult to do, but exactly how depends on what you used to display the Login form in the first place.

1) If you used ShowDialog then it's simple: Call Close on the Login form when you press the button and the code will continue in Form1 from after the ShowDialog call. Form1 can then Hide or Close itself and display the MainPage instead.

2) If you used Show then it's also pretty simple - it just sounds complicated. All you need to do is create an Event in your Login form, which is handled in Form1 and which does everything as described in (1) above. That's really easy to do: Transferring information between two forms, Part 2: Child to Parent[^] will show you how.

Probably, you want to Hide Form1, not close it - because if Form1 is the startup form then closing it closes the application so you would never see MainPage at all! But if you do, then you need to handle the MainPage.FormClosed event and close Form1 or the app will never exit (but the user will never be able to see it!)

And do yourself a couple of favours:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
3) Stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


这篇关于如何在C#中关闭或隐藏表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆