2

I am writing a simple WindowsForm program with radiobuttons, buttons, and a customary implemented InputBox.

Relevant program logic: I click one of the radiobutons from the groupbox -> enable button -> clicking button initiates custom inputbox asking for 1 value and Yes/Cancel buttons.

|
V

If Yes button clicked -> proceed with logic flow

If Cancel button clicked -> popup window appears ("Your input was cancelled, do you want to exit application?") with yes/no buttons

    |
    V

If Yes button clicked -> Exit the program

If No button clicked -> apply the same logic as if Reset button is clicked, which would reset the whole program without restarting it <---------This is what I am trying to achieve (all relevant methods are provided below)

What is the problem?

When I simply click Reset button, it applies all needed actions and stops, waiting for my further input. This is the exact result I am trying to achieve when I click No button in the popup window I mentioned above.

However, this is not the case. During the debug mode, after I clicked No button, it goes through the entire logic of the Reset button like I wanted, BUT right after that, it goes into the IF statement (marked in my buttonGetWorkHours_Click method). I don't want that, I want it to stop the flow after applying the logic of Reset button and waiting for my input (radiobutton/button click).

What I tried

I have searched through several threads here in SO and tried implementing several suggestions. The results of these suggestions are commented out inside inputBoxProcedure method. Also, I was looking for similar posts, which would give me the right idea. But they state that it is impossible without reloading. Based on that thread, I thought about implementing extra variable to use for checking if reset logic was running, but seems unnecessarily complicated.

ULTIMATE QUESTION:

I saw a test executable, so I know it is POSSIBLE, unlike what posts and threads were saying. Can you please point me in a right direction of how to proceed with it?

Relevant code snippet methods

For the sake of saving everyone's time I will include only methods relevant to my question.

private void buttonGetWorkHours_Click(object sender, EventArgs e)
  {
     if (radioButtonJobStatusFull.Checked) 
     {
        inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
     }
     else if (radioButtonJobStatusPart.Checked)
     {
        inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
     }

     //if, else if, else, unrelated to the lines above
     if()    //<----------------the logic goes here after going through the whole Reset button logic
     {}
     else if()
     {}
     else()
     {}
  }

private void buttonReset_Click(object sender, EventArgs e)
  {
      //clear all radiobutton selections
      //set all strings to empty
      //disable several buttons and groupboxes
      //hide several labels
  }

private void inputBoxProcedure(string text, string defaulttext)
  {
     InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
     if (result.OK)
     {
        labelHoursWorked.Text = result.Text.Trim();
     }
     else
     {
        if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
           Close();
        }
        else
        {
           buttonReset_Click(this, new EventArgs());
           //Form fr = new Form();
           //fr.Show();
           //this.Close();
           //Application.Restart();
        }
     }
  }
Community
  • 1
  • 1
Vadzim Savenok
  • 930
  • 3
  • 14
  • 37

1 Answers1

1

Try changing the inputBoxProcedure to this:

private bool inputBoxProcedure(string text, string defaulttext)
{
    InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
    if (result.OK)
    {
        labelHoursWorked.Text = result.Text.Trim();
    }
    else
    {
        if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            Close();
        }
        else
        {
            buttonReset_Click(this, new EventArgs());
            //Form fr = new Form();
            //fr.Show();
            //this.Close();
            //Application.Restart();

            return false; // Added
        }
    }

    return true; // Added
}

Notice that the return type void becomes bool and that two lines of return have been added.

In buttonGetWorkHours_Click change:

if (radioButtonJobStatusFull.Checked)
{
    inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
}
else if (radioButtonJobStatusPart.Checked)
{
    inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
}

Into:

if (radioButtonJobStatusFull.Checked)
{
    if (!inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40"))
        return;
}
else if (radioButtonJobStatusPart.Checked)
{
    if (!inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30"))
        return;
}

In this way, after a reset, the funtion inputBoxProcedure will return false. When the function returns false the function buttonGetWorkHours_Click will return and thus prevent further excecution.

I hope this helps.

MasterXD
  • 804
  • 1
  • 11
  • 18