为什么模态形式在最小化时会完全消失?
我试图在模式形式最小化时使所有者形式最小化。但是当我最小化情态形式时——它就完全消失了。 (- 我可以点击所有者表格。)
我该如何解决这个问题?
我有:
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.Owner = this;
}
private void button1_Click(object sender, EventArgs e)
{
frm2.ShowDialog();
}
}
并且:(
class Form2 : Form
{
Form1 frm1;
FormWindowState ws = new FormWindowState();
public Form2()
{
SizeChanged += new EventHandler(Form2_SizeChanged);
}
void Form2_SizeChanged(object sender, EventArgs e)
{
frm1 = (Form1)Owner;
if (WindowState == FormWindowState.Minimized)
{
ws = frm1.WindowState;
frm1.WindowState = FormWindowState.Minimized;
}
else frm1.WindowState = ws;
}
}
在尝试这个时,我也遇到了这个:模态表单只有最小化并且所有者表单被单击一次后才会出现在托盘中。如何让它出现? )
I'm trying to have the owner-form minimize when the modal-form is minimized. But when I minimize the modal-form – it disappears completely. (- I can click on the owner-form.)
How do I solve this?
I have:
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.Owner = this;
}
private void button1_Click(object sender, EventArgs e)
{
frm2.ShowDialog();
}
}
And:
class Form2 : Form
{
Form1 frm1;
FormWindowState ws = new FormWindowState();
public Form2()
{
SizeChanged += new EventHandler(Form2_SizeChanged);
}
void Form2_SizeChanged(object sender, EventArgs e)
{
frm1 = (Form1)Owner;
if (WindowState == FormWindowState.Minimized)
{
ws = frm1.WindowState;
frm1.WindowState = FormWindowState.Minimized;
}
else frm1.WindowState = ws;
}
}
(While trying this, I also ran into this: Modal form doesn't appear in tray until minimized and owner-form is clicked once. How do I make it appear? )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是设计使然。作为模态契约的一部分,显示对话框会禁用应用程序中的所有其他窗口。当用户最小化对话框窗口时,将不再有用户可以访问的窗口。导致应用程序无法使用。 Winforms 通过在对话框最小化时自动关闭对话框来确保这种情况不会发生。
显然你会想完全阻止这种情况的发生。将 MinimizeBox 属性设置为 false。 MaximizeBox 属性也应该设置为 false,使两个按钮从窗口标题中消失。顺便说一句,为帮助按钮留出空间。
This is by design. As part of the modality contract, showing a dialog disables all the other windows in the application. When the user minimizes the dialog window, there are no windows left that the user can access. Making the app unusable. Winforms ensures this cannot happen by automatically closing the dialog when it gets minimized.
Clearly you'll want to prevent this from happening at all. Set the MinimizeBox property to false. The MaximizeBox property ought to be set to false as well, making both buttons disappear from the window caption. Leaving room for the HelpButton btw.
我不记得每个人都需要这么多代码才能让模态 Windows 工作。我对您的评论“我可以单击所有者表单”感到担忧,这使我相信该表单未正确设置为模式。根据定义,必须先处理模态表单,然后用户控件才能返回所有者表单。最小化情态形式并不构成正确“处理”情态形式。
这是我过去使用过的一些代码。注意:在 ShowDialog 中将所有者作为参数传递会建立所有权关系。虽然我怀疑你的代码可以工作,但我并没有这样使用它。
另外,当我完成此操作时,我没有在模式表单中放置任何特殊代码,并且还禁用了表单右上角的所有按钮;从而确保用户无法关闭、最小化或最大化我提供的任何按钮之外的模式表单。
我希望这有帮助。
I don't recall every needing this much code to get modal Windows to work. I'm concerned by your comment 'I can click on the owner form', which leads me to believe that the form is nt being correctly set up as modal. By defintion, modal forms must be dealt with before user control can return to the owner form. Minimizinfg the modal form does not constitute properly 'dealing' with the modal form.
Here is some code that I have used in the past. Notes: passing the owner as parameter in ShowDialog establishes the ownership relationship. While I suspect your code works, I've not used it that way.
Also, when I have done this, I have not put any special code in the modal form, and have also disabled all the button in the upper right corner of the form; thereby insuring that the user cannot close, minimize, or maximize the modal form outside of any buttons I have provided.
I hope this helps.
添加:
在 Form2 事件处理程序的末尾。
Add a:
At the end of Form2's event-handler.
表单有一个属性
ShowInTaskbar
。如果将其设置为false
,那么即使最小化,表单也永远不会出现在任务栏上。Forms have a property
ShowInTaskbar
. If it is set tofalse
then the form will never appear on the task bar, even when minimized.我还要求在最小化对话框表单时应最小化应用程序,而在恢复应用程序时应再次显示对话框。这是我所做的:
MainForm.cs
Form2.cs
I also had the requirement where when minimizing a dialog form it should minimize the application and when restoring the application it should show the dialog again. Here's what I did:
MainForm.cs
Form2.cs