Question

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

Était-ce utile?

La 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
}

Autres conseils

For pure modal dialogs, a useful pattern is to create a object to hold the result. e.g.

public class InputBox
{
  public class Result //
  (
    DialogResult : ModalResult {get; private set;}
    DateTime : ShutDown {get; private set;}
    ... // more fields as needed
  )
}

public Show()
{
  var result = new Result(); // do not store within form -- prevent G/C of form
  using (myform = new InputBox())
  {
      // pseudocode
      // myform.Initialize();
      // 
      result.ModalResult = myform.ShowDialog(...);  // return
      result.ShowDownTime = myform.controlShutDownTime.Value;
      // myform.Finalize();
  }
}

}

In the modal dialog class, add a public static method that creates the modal form, invokes it and then stuffs the inner Result object before returning (Show() in the pseudocode above). You can have multiple versions of the methods if needed.

if(InputBox.shutdownTime(title, prompt, ref stringresult)==DialogResult.Cancel)
{

 /// DO CANCEL STUFF

}
else
{

  // DO OK STUFF  

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top