Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
`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

C#
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(Control form) will reset all controls but can not fill combo box for that i need to call form constructor
Posted

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?
 
Share this answer
 
Comments
Chetan Saini 26-Jun-14 4:00am    
above function is common for all the forms, and control feeling is different for all the forms.
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!
 
Share this answer
 
Comments
Chetan Saini 26-Jun-14 4:03am    
problem with above solution i had done all the form filling coding in my constructor and changing code and can be problematic.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900