0

I'm trying to figure out how I can reset my windows form application once the option to do so has been selected from a choice of to radio buttons. eg:.

selection

So once "Yes" is selected the program resets. Here is what I have been trying.

private void EndOptions() {                  

        if (anotheryes.Checked) {
            RestartForm(); 
        }

        if (anotherno.Checked) {

            //todo
        }
    }

The restartform method is just:

private void RestartForm() {
        Application.Restart();
        Application.ExitThread();
    }

Right now, this only works if I press the "calculate ratio" button again. On click, the application will restart. I'm wanting it so that it resets just by clicking the Yes radiobutton.

The logic behind the button is as follows:

 private void DisplayLogic() {

        double height = 0, waist = 0;

        if (WaistNumericCheck(out waist) && HeightNumericCheck(out height)) { //check if input is numeric, if true, move on to next check

            if (CheckDimensionHeight(height) == true && CheckDimensionsWaist(waist) == true) { //check if dimensions are higher than the lower limits, if BOTH true, move on the next

                ShowResult(height, waist); //shows results
                DisableInputs(); //disables inputs while results are being shows
                AnotherGroupBoxVisible();//makes the restart selection groupbox visible
                EndOptions(); //check to see which option is selected                    
            }
        }
    }

Any tips/help would be greatly appreciated thanks!

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
user3496101
  • 303
  • 1
  • 6
  • 14
  • 1
    You're taking the lazy option. Just write some code to put your application in an appropriate state. If you have to set or reset some variables then do that. Restarting the application altogether is just a hack. – jmcilhinney Sep 16 '14 at 05:54

2 Answers2

0

I have to say its an odd thing to have the entire app restart, just so the user could do another calculation.. The API you are using i.e. Application.Restart is documented for use with ClickOnce Applications http://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart(v=vs.110).aspx

I don't know if your app is click once or not.. but anyway, hoping you'd learn something new regardless, here is literal answer to your question..

http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx

This event occurs when the value of the Checked property of a radio button changes.

Anyway, I do strongly recommend, that as suggested in comment above, you should reconsider how you want your app to provide a better user experience here..

If its just for learning, the event should help.. and feel free to explore other events on the control which might be of interest.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
0

Handle the CheckedChanged event of the RadioButton, and then you can immediately restart as soon as it is clicked:

    private void AnotherYes_CheckedChanged(object sender, EventArgs e)
    {
        if (AnotherYes.Checked)
        {
            Application.Restart();
        }
    }

http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx

Sean Skelly
  • 1,229
  • 7
  • 13