【问题标题】:Stop executing code and wait for a button to be clicked停止执行代码并等待按钮被点击
【发布时间】:2020-09-30 11:30:46
【问题描述】:

我以这样一种方式设计我的应用程序,即“错误”屏幕是一个控件,它显示在任何其他控件之上。

public Alert(Control control,string message, Stages stage)
        {
            InitializeComponent();

            control.Controls.Add(this);
            Size = new Size(579, 524);
        }

        public static void Show(Control control,string message, Stages stage)
        {
            new Alert(control, message, stage).Show();
        }

但是,当显示错误屏幕时,代码会继续执行,我不希望这样。我不想使用 Form 而不是 Control 来使用 .ShowDialog() 功能。错误屏幕有一个按钮,单击该按钮应恢复代码。

有什么想法吗?

【问题讨论】:

  • “代码继续执行”是什么意思? ...其他控件和窗口仍然对输入做出反应或可以集中注意力? (所以......你想要某种没有对话框的模态对话框?)
  • 为什么不将警报创建为表单并使用“ShowDialog”?这就是它的预期用途。

标签: c# winforms controls


【解决方案1】:

您所做的不是很标准,因此没有快速简便的方法。您几乎只需要禁用其他所有功能。

public static void Show(Control control,string message, Stages stage)
{
    new Alert(control, message, stage).Show();
    var otherControls = control.Parent.Controls.OfType<Control>().Where( x => x != control).ToList();
    foreach (var c in otherControls)
    {
        c.Enabled = false;
    }
}

当然你必须在之后重新启用它们,当然要注意不要重新启用一开始就没有启用的控件。

更常见的解决方案是仅在顶部显示错误消息而不禁用其他任何内容。这对最终用户来说更容易,因为他们无需额外点击即可继续工作。但是如果你真的想阻止用户,对话框是标准的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 2013-06-21
    • 1970-01-01
    • 2014-04-09
    • 2020-12-12
    • 2017-10-23
    • 2014-02-24
    • 1970-01-01
    相关资源
    最近更新 更多