【问题标题】:Returning a dialogResult from method to use in another. c#从方法返回一个 dialogResult 以在另一个方法中使用。 C#
【发布时间】:2013-12-28 10:22:03
【问题描述】:

我会尽力解释。

我正在使用一个类 InputBox 并使用一个方法 shutownTime

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

我正在返回值

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

在另一种方法中,我将值从另一个类传递给它。

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);

一切正常,但是当我按下 OK 按钮时,如果我按下 shutdownTime 方法中的 Cancel 按钮,调试会显示返回值为 Cancel

//shutdownTime()

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

我无法得到的是,如果我按取消,我如何在使用后检查该值

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

因为我可以按确定或取消,它只是继续代码。

希望我说得通,谢谢

【问题讨论】:

    标签: c# dialogresult


    【解决方案1】:

    存储从shutdowTime()返回的值

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

    然后测试那个值

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

    【讨论】:

      【解决方案2】:

      对于纯模式对话框,一个有用的模式是创建一个对象来保存结果。例如

      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();
        }
      }
      

      }

      在模态对话框类中,添加一个公共静态方法,该方法创建模态表单,调用它,然后在返回之前填充内部 Result 对象(上面伪代码中的 Show())。如果需要,您可以拥有多个版本的方法。

      【讨论】:

        【解决方案3】:
        if(InputBox.shutdownTime(title, prompt, ref stringresult)==DialogResult.Cancel)
        {
        
         /// DO CANCEL STUFF
        
        }
        else
        {
        
          // DO OK STUFF  
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-01
          • 2016-12-27
          • 2022-09-29
          • 2021-07-07
          • 1970-01-01
          • 1970-01-01
          • 2011-09-27
          相关资源
          最近更新 更多