Search code examples
c#dialogresult

Returning a dialogResult from method to use in another. c#


I will try and explain the best I can.

I am using a class InputBox and using a method shutownTime

class InputBox
{
    public static DialogResult shutdownTime(string title, string promptText, ref string value)
    {

and I am returning value

DialogResult dialogResult = form.ShowDialog();      
return dialogResult;

and in another method I am passing the values to it from another class.

string title2 = "Shutdown Timer";
string promptText = "How long would you like to delay the shutdown in minutes?";
string value = "0";          
InputBox.shutdownTime(title2, promptText, ref value);

it all works fine but when I press the OK button, but if I press the Cancel button from the shutdownTime method the debug shows that the return value is Cancel

//shutdownTime()

buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;

What I can't get is if I press Cancel how can I check that value after using

InputBox.shutdownTime(title2, promptText, ref value);

because I can press OK or Cancel and it just carries on the code.

Hope I have made sense, thanks


Solution

  • Store the value returned from shutdowTime()

    var result = InputBox.shutdownTime(title2, promptText, ref value);
    

    And then test that value

    if (result == DialogResult.OK)
    {
        // Do something
    }