Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当子窗口打开时,如何在C#中禁用父窗口?_C#_Wpf_Window_Showdialog - Fatal编程技术网

当子窗口打开时,如何在C#中禁用父窗口?

当子窗口打开时,如何在C#中禁用父窗口?,c#,wpf,window,showdialog,C#,Wpf,Window,Showdialog,我创建了一个计时器类,用于监视软件的许可证。发生错误时,我调用ShowDialog()以显示自定义的windows窗体。我的问题是如何禁用父窗口?这是我的问题的一个简单例子。正如您可以看到的那样,一旦MessageBox弹出,您仍然可以从MainForm窗口进行键入 MainForm1.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

我创建了一个计时器类,用于监视软件的许可证。发生错误时,我调用ShowDialog()以显示自定义的windows窗体。我的问题是如何禁用父窗口?这是我的问题的一个简单例子。正如您可以看到的那样,一旦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();  
        }  
    }  
}  
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;  
        }  
    }  
}  
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.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();  
        }  
    }  
}  
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;  
        }  
    }  
}  
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();  
        }  
    }  
}  
TimerClass1.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();  
        }  
    }  
}  
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;  
        }  
    }  
}  
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();  
        }  
    }  
}  

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

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

您将在单独的线程上显示
消息框,因此它不会显示为主窗口的模式对话框。您需要在主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();  
    }  

您将在单独的线程上显示
消息框
,因此它不会显示为主窗口的模式对话框。您需要在主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();  
    }  

要解决此问题,只需更改以下内容:

TimerClass1.cs文件

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

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

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

TimerClass1.cs文件

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

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

父窗体作为参数发送到计时器,并显示如下对话框,这应该是一个技巧:

MainForm1.cs文件

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

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

父窗体作为参数发送到计时器,并显示如下对话框,应该可以实现以下目的:

MainForm1.cs文件

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

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

可能重复?:仅从阅读此代码,我会说主窗口应该是无响应的。但是,创建自己的MessageBox类是一件令人困惑的事情。另外,为什么计时器要调用MessageBox?如果您想让showdialog工作,应该从mainform调用它。可能重复?:仅从阅读此代码,我会说主窗口应该是无响应的。但是,创建自己的MessageBox类是一件令人困惑的事情。另外,为什么计时器要调用MessageBox?如果您想让showdialog工作,应该从主窗体调用它。或者只使用更合理的计时器类。或者只使用更合理的计时器类。这是WPF应用程序,而不是WinForms应用程序;他应该使用
System.Windows.Threading.dispatchermer
OK,答案基本不变。这是WPF应用程序,不是WinForms应用程序;他应该使用
System.Windows.Threading.dispatchermer
OK,答案基本不变。