将bool值返回到父表单 [英] return bool value to parent form

查看:83
本文介绍了将bool值返回到父表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种形式,

-第一种形式是主要的,当点击按钮时,显示第二种形式

-第二种形式要求输入密码

-if密码有效,然后转到第三个表格



Form1

i have 3 forms,
-first form is main, when button is clicked, show second form
-second form ask for password
-if password is valid, then go to third form

Form1

private void btnProfile_Click(object sender, EventArgs e)
{
   Form2 pass = new Form2 ("Form3");
   pass.ShowDialog();
}





Form2



Form2

private void btnProfile_Click(object sender, EventArgs e)
{
 if (checkPassword() == true)
 {
   Form form = (Form)Activator.CreateInstance(Type.GetType("Form3"));
   form.ShowDialog();
 }
}





现在,上面的代码可以作为魅力。我需要修改,以便Form2将Bool值返回到Form1。

- 如果有效密码,则返回true到Form1

- 如果不是有效密码,则返回false给Form1



Now, the code above is work as charm. i need to modify so that the Form2 will return the Bool value to Form1.
- if valid password, return true to Form1
- if not a valid password, return false to Form1

推荐答案

嗨melberry,



我不会建议使用布尔值。对于这种情况,你有 DialogResult 枚举。



所以做这样的事情:



Hi melberry,

I wouldn't recomend to use a boolean. For this case you have the DialogResult enumeration.

So do something like this:

if(form.ShowDialog() == DialogResult.Ok)
{
   // Go to 3rd form
}
else
{
   // Invalid or Dialog was canceled
}





这种方法最好与 DialogResult一起使用/ code> System.Windows.Forms.Button 的属性。或者您可以在设计器中设置表单的 AcceptButton CancelButton 属性以连接按钮。

当然,您可以随时在代码中手动设置表单的 DialogResult 属性。



这个信息对你有帮助吗?



亲切的问候约翰内斯



this approach is best used together with the DialogResult property of a System.Windows.Forms.Button. Or you can set the AcceptButton, CancelButton properties of your form in the designer to wire up your buttons.
Of course you can always set the DialogResult property of your form manually in code.

Does this info help you?

Kind regards Johannes


首先,这段代码有惊人的滥用。要创建 Form3 类型的表单实例,您需要执行 Form myForm = new Form3(); 没有别的。使用 Activator 你的方式完全没有意义且完全不受支持:如果你改变了类型名称,编译器将不会在 Type.GetType(Form3)拼写错误。此外,您永远不应使用自动生成的名称,如 Form3 或btnProfile_Click。所有名称都应该是语义敏感的。您如何看待为什么使用Visual Studio重构引擎?



现在,表单之间的子父关系被有效禁用。您的所有表格都不是任何其他表格的父母。如果您尝试为表单设置 Parent 属性,它将通过异常(有一个记录的解决方法,但我不知道它是如何有用的人们表示不想做任何虐待)。但是你真的需要使用属性所有者,它与同一个应用程序的表格的Z顺序有关:它将所有这些形式保存在Z顺序中,这是对UI完整性很重要。无论如何,它已经与你的主要问题有关。



看起来你的问题与关于表单协作的流行问题有关。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
First of all, this code has amazing abuse. To create a form instance of the type Form3, you need to do Form myForm = new Form3(); nothing else. Using Activator the way you do it totally pointless and totally unsupportable: should you ever change the type name, the compiler won't detect that "Form3" in Type.GetType("Form3") is misspelled. Besides, you should never use auto-generated names like Form3 or btnProfile_Click. All names should be semantically sensitive. How do you think why are you given a refactorizatoin engine with Visual Studio?

Now, child-parent relationships between forms are effectively disabled. None of your forms is a parent of any other forms. If you try to set the Parent property for a form, it will through exception (there is a documented work-around, but I don't know how it can be useful for people show don't want doing any abuse). But you really need to use the property Owner, which is related to Z-order of forms of the same application: it keeps all such forms together in Z-order, which is important for UI integrity. Anyway, it has noting to do with your main problem.

It looks like your problem is related to the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于将bool值返回到父表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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