如何从win表单对象调用windows表单构造函数。 [英] How to call windows form constructor from win form object.

查看:108
本文介绍了如何从win表单对象调用windows表单构造函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个秋天我接受我做了不好的编码练习,但现在我需要配合它。



在点击提交按钮的赢取表格中,我打电话消息框中带有

First fall i accept i had done bad coding practice but now i need to go with it.

In my win form on click of submit button, i call message box with

`YES,NO,CANCEL` 

单击无按钮表单上的按钮应该关闭,单击是按钮表单构造函数应该再次调用,因为在构造函数中只有我我在做组合框控制填充。我要求提交的功能



button on click of no button form should close and on click of yes button form constructor should call again because in constructor only i am doing combo box control filling. Function i am calling on submit

public static void showMessage(int rowsaffected, string sucessMsg, string failedMsg, Form form, bool[] a )
     {
         if (rowsaffected > 0)
         {
        DialogResult result = MessageBox.Show(sucessMsg+", Would You Like To Add More Item", "Confirmation",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question,
        MessageBoxDefaultButton.Button2);

             if (result == DialogResult.Yes)
             {
                 for (int i = 0; i < a.Length; i++)
                     a[i] = false;
                 ResetAllControls(form);
             }
             else
             {
                 form.Close();
             }

         }
         else
         {
             MessageBox.Show(failedMsg);
         }
     }





ResetAllControls(控制表格)将重置所有控件但无法填写我需要调用表单构造函数的组合框



ResetAllControls(Control form) will reset all controls but can not fill combo box for that i need to call form constructor

推荐答案

为什么不将填充组合框的代码移动到私有方法中,然后从构造函数中调用该方法,从ResetAllControls方法?
Why not move the code that fills your combo box into a private method, then call that method from the constructor and from the ResetAllControls method?


麻烦的是关闭表单可能有副作用:如果它是主窗体,那么它将自动关闭应用程序。这是一件糟糕的事情......:笑:

有很多方法,但他们需要表格来了解它的工作环境,或者在表格之外的代码做出反应用户对表单的输入 - 两者都不理想。



我没有关闭表单并尝试重新创建它,而是更改构造函数以便它调用了一个方法来填充你的组合框,然后当用户按下是时调用该方法 - 从OOP的角度来看,这个方法要好得多,对用户来说也可能更好!
Trouble is that closing a form can have side effects: if it is the main form then it will automatically close the application. Which is a Bad Thing... :laugh:
There are ways round that, but they need the form to know about the environment in which it works, or to have code outside the form which reacts to the user input to the form - neither of which is ideal.

Instead of closing the form and trying to re-create it, I'd change the constructor so that it called a method which filled your combo-box and then also call that method when the user presses "Yes" - it's a lot better from an OOPs point of view, and probably better for the user as well!


这篇关于如何从win表单对象调用windows表单构造函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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