【问题标题】:How to return the value by DialogResult.OK by not to closing the form如何通过 DialogResult.OK 通过不关闭表单返回值
【发布时间】:2015-09-23 14:22:23
【问题描述】:

我创建了一个带有打印预览的打印工具。打印预览是按表格制作的。我想让用户在预览表单未关闭时单击打印按钮来打印文档。

如何将DialogResult.OK 返回到打印工具以防止表单消失?

【问题讨论】:

  • 我建议了一种实现你的req的方法,如果有不清楚的地方可以在这​​里问我
  • 你能教我方法吗?

标签: c# forms dialogresult


【解决方案1】:

你不能。

DialogResult 与模态窗口一起使用。模态窗口基本上劫持了底层的 UI 消息循环,这使得它们相对于调用者是同步的。

如果您需要打印预览来启动打印,同时保持对话框模式,只需给它一种启动打印的方法,而不是让调用者对返回的DialogResult 做出反应。可能最简单的方法是简单地将Action 委托传递给对话框 - 当按下 OK 时,您调用委托。

【讨论】:

    【解决方案2】:

    在 C# 中没有像 i 现在这样的功能。但是,您可以创建一个自定义对话框来执行此操作。

    public static class MyDialog
    {
        public static int ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 100;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top=20, Text=text };
            NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
            Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
    
            confirmation.Click += (sender, e) => { //YOUR FUNCTIONALITY };
    
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputBox);
            prompt.ShowDialog();
    
            return (int)inputBox.Value;
        }
    }
    

    然后调用它:

     int MyDialogValue = MyDialog.ShowDialog("Test", "123");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 2012-09-22
      • 2021-09-05
      • 1970-01-01
      • 2015-10-19
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多