【问题标题】:How can I disable the parent window in C# when the child window is open?当子窗口打开时,如何在 C# 中禁用父窗口?
【发布时间】:2012-08-14 20:23:37
【问题描述】:

我创建了一个计时器类,用于监控软件的许可证。当发生错误时,我调用 ShowDialog() 来显示我的自定义窗口窗体。我的问题是如何禁用父窗口?这是我的问题的一个简单示例。正如您所看到的,一旦 MessageBox 弹出,您仍然可以在 MainForm 窗口中输入。

MainForm1.cs 文件

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  

namespace TestProject  
{  
    public partial class MainForm1 : Form  
    {  
        public MainForm1()  
        {  
            InitializeComponent();  
        }  

        private void MainForm1_Load(object sender, EventArgs e)  
        {  
            TimerClass1 timer = new TimerClass1();  
        }  
    }  
}  

MessageBox.cs 文件

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  

namespace TestProject  
{  
    public partial class MessageBox : Form  
    {  
        public MessageBox()  
        {  
            InitializeComponent();  
            this.label1.Text = "Hello There";  
            this.button1.Text = "OK";  
            this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;  
        }  
    }  
}  

TimerClass1.cs 文件

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Timers;  
using System.Windows;  

namespace TestProject  
{  
    class TimerClass1  
    {  
        Timer _timer;  
        public TimerClass1()  
        {  
            _timer = new Timer(1);  
            _timer.Elapsed +=new ElapsedEventHandler(_timer_Elapsed);  
            _timer.Enabled = true;  
        }  

        private void _timer_Elapsed(object sender, ElapsedEventArgs e)  
        {  
            _timer.Stop();  
            MessageBox msg = new MessageBox();  
            msg.ShowDialog();  
            _timer.Start();  
        }  
    }  
}  

【问题讨论】:

  • 仅仅通过阅读这段代码,我会说主窗口应该是无响应的。但是制作你自己的 MessageBox 类是令人困惑的事情。
  • 另外,为什么定时器调用消息框?如果你想让 showdialog 工作,应该从主窗体调用它。

标签: c# wpf window showdialog


【解决方案1】:

您在单独的线程上显示MessageBox,因此它不会显示为主窗口的模式对话框。您需要在主 UI 线程上显示它:

    private void _timer_Elapsed(object sender, ElapsedEventArgs e)  
    {  
        _timer.Stop();  
        Application.Current.Dispatcher.Invoke(new Action(
        () => {
            MessageBox msg = new MessageBox();  
            msg.ShowDialog();  
        }));
        _timer.Start();  
    }  

【讨论】:

  • 或者只是使用更明智的 Timer 类。
【解决方案2】:

要修复它,只需更改以下内容:

TimerClass1.cs 文件

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
//using System.Timers;  
using System.Windows;  

然后修复因切换到 Windows.Forms.Timer(您需要的那个)而导致的错误。

【讨论】:

  • 这是一个 WPF 应用程序,而不是一个 WinForms 应用程序;他应该使用System.Windows.Threading.DispatcherTimer
  • 好的,答案基本保持不变。
【解决方案3】:

您需要某种方法从 TimerClass1 访问 MainForm1。完成此操作后,您可以在 MainForm1 上创建并调用一个方法,该方法将禁用表单本身或表单上的控件。

【讨论】:

    【解决方案4】:

    父表单作为参数发送到您的计时器,并显示如下对话框应该可以解决问题:

    MainForm1.cs 文件

    TimerClass1 timer = new TimerClass1(this);
    

    TimerClass1.cs 文件

    ..
    private Form ParentForm {get; set;}
    ..
    public TimerClass1(Form parentForm)           
    {
    ..
    this.ParentForm = parentForm;
    ..   
    }
    ..
    private void _timer_Elapsed(object sender, ElapsedEventArgs e)           
    {
    ..
     msg.ShowDialog(this.ParentForm);
    ..
    } 
    .. 
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多