如果输入的信息正确,则关闭第一份表格

编程入门 行业动态 更新时间:2024-10-26 07:24:58
本文介绍了如果输入的信息正确,则关闭第一份表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 在一个程序中,我有2种形式.第一个用于从用户获取用户名"和密码".如果输入的信息正确,则显示第二个表格.现在,我想关闭第一个表单,或者至少隐藏它不被看到,因此我使用了下面的代码,但是它没有关闭或隐藏第一个表单.您能告诉我如何使它工作吗? 非常感谢

Hello, In a program I have 2 forms. First one is used to get "Username" and "Password" from the user. If the entered information is correct, the second form is shown. Now I want to close the first form or at least hide it not to be seen so I used the code below but it did not close or hide the first form. Could you please let me know how could I make it work? Thank you a lot

private void button1_Click(object sender, EventArgs e) { string InputUserName = "Ali"; string InputPassword = "4862597k"; string strUserName = textBox1.Text; string strPassword = maskedTextBox1.Text; if (strPassword == InputPassword && strUserName == InputUserName ) { Form2 f2 = new Form2(); f2.Show(); Form1 f1 = new Form1(); f1.Hide(); // f1.Close(); } else { MessageBox.Show("Wrong Password or Username, Try again.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning); maskedTextBox1.Text = ""; } }

推荐答案

好吧,当您这样做时: Well, when you do this: Form2 f2 = new Form2(); f2.Show(); Form1 f1 = new Form1(); f1.Hide(); // f1.Close();

您正在创建两个新表单,分别显示一个表单和另一个表单.您是否要隐藏已经显示的表单,而不是隐藏刚刚创建的新表单?我认为您的意思是this.Hide()而不是创建新的Form1. 另外,请参见此 [ ^ ]的答案,我曾在一个非常相似的问题上发表过.它用模糊的术语描述了用于创建和显示登录表单的更好的解决方案.

you''re creating two new forms, showing one and hiding the other. Don''t you want to hide the form that''s already shown instead of hiding a new one that you just created? I think you meant this.Hide() instead of making a new Form1. Also, see this[^] answer that I posted to a very similar question. It describes in vague terms a better solution for creating and showing a login form.

您应该将其替换为您的代码 如果(strPassword == InputPassword&&strUserName == InputUserName { Form2 f2 =新的Form2(); f2.Show(); this.Hide(); /////// } 只需尝试一下. you should replace this with ur code if (strPassword == InputPassword && strUserName == InputUserName { Form2 f2 = new Form2(); f2.Show(); this.Hide(); /////// } just try it .

对于您采用的方法,我可能建议您在要远程"关闭的表单上添加一个名为CloseForm()的公共方法.如果需要,这将允许您执行其他关闭项,这些逻辑应该在该类上,而不是在该类上. 当您创建Form1的实例以显示用户名/密码对话框时,请保留对其的引用.使用引用来调用CloseForm()方法(而不是像上面那样创建一个新方法). 干杯. For the approach you''re taking, I would likely suggest adding a public method called CloseForm() on the form you want to close "remotely". This will allow you to do other shutdown items if required down the road, logic that should be on that class and not this one. When you create an instance of Form1 to show the username/password dialog, keep a reference to it. Use the reference to invoke the CloseForm() method (rather than creating a new one as you do above). Cheers.

更多推荐

如果输入的信息正确,则关闭第一份表格

本文发布于:2023-11-17 03:32:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一份   表格   正确   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!