C# 当子表单处于活动状态时如何禁用父表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1130208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable the parent form when a child form is active?
提问by
How to disable a parent form when child form is active using c#?
c# - 当子表单处于活动状态时,如何禁用父表单?
回答by R. Martinho Fernandes
Have you tried using Form.ShowDialog()instead of Form.Show()?
您是否尝试过使用Form.ShowDialog()而不是Form.Show()?
ShowDialog shows your window as modal, which means you cannot interact with the parent form until it closes.
ShowDialog 将您的窗口显示为模态,这意味着您在关闭之前无法与父窗体交互。
回答by Philip
Are you calling ShowDialog()
or just Show()
on your child form from the parent form?
您ShowDialog()
是Show()
从父表单调用还是仅在您的子表单上?
ShowDialog
will "block" the user from interacting with the form which is passed as a parameter to ShowDialog
.
ShowDialog
将“阻止”用户与作为参数传递给ShowDialog
.
Within the parent you might call something like:
在父级中,您可能会调用以下内容:
MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);
where this
is the parent form.
this
父窗体在哪里。
回答by Fredrik M?rk
What you coulddo, is to make sure to pass the parent form as the owner when showing the child form:
您可以做的是确保在显示子表单时将父表单作为所有者传递:
Form newForm = new ChildForm();
newForm.Show(this);
Then, in the child form, set up event handlers for the Activated
and Deactivate
events:
然后,在子窗体中,为Activated
和Deactivate
事件设置事件处理程序:
private void Form_Activated(object sender, System.EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Enabled = false;
}
}
private void Form_Deactivate(object sender, System.EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Enabled = true;
}
}
However, this will result in a truly wierd behaviour; while you will not be able to go back and interact with the parent form immediately, activating any other application will enable it, and then the user can interact with it.
然而,这将导致真正奇怪的行为;虽然您将无法立即返回并与父表单交互,但激活任何其他应用程序将启用它,然后用户可以与其交互。
If you want to make the child form modal, use ShowDialog
instead:
如果要使子窗体modal,请ShowDialog
改用:
Form newForm = new ChildForm();
newForm.ShowDialog(this);
回答by electriac
Why not just have the parent wait for the child to close. This is more than you need.
为什么不让父母等待孩子关闭。这超出了您的需要。
// Execute child process
System.Diagnostics.Process proc =
System.Diagnostics.Process.Start("notepad.exe");
proc.WaitForExit();
回答by helgeheldre
While using the previously mentioned childForm.ShowDialog(this) will disable your main form, it still doesent look very disabled. However if you call Enabled = false before ShowDialog() and Enable = true after you call ShowDialog() the main form will even look like it is disabled.
虽然使用前面提到的 childForm.ShowDialog(this) 将禁用您的主窗体,但它看起来仍然非常禁用。但是,如果您在 ShowDialog() 之前调用 Enabled = false 并且在调用 ShowDialog() 之后调用 Enable = true,则主窗体甚至看起来像是被禁用了。
var childForm = new Form();
Enabled = false;
childForm .ShowDialog(this);
Enabled = true;
回答by stack
ChildForm child = new ChildForm();
child.Owner = this;
child.Show();
// In ChildForm_Load:
// 在 ChildForm_Load 中:
private void ChildForm_Load(object sender, EventArgs e)
{
this.Owner.Enabled = false;
}
private void ChildForm_Closed(object sender, EventArgs e)
{
this.Owner.Enabled = true;
}
来源:http: //social.msdn.microsoft.com/Forums/vstudio/en-US/ae8ef4ef-3ac9-43d2-b883-20abd34f0e55/how-can-i-open-a-child-window-and-block-仅父窗口
回答by sk2185
Form1 frmnew = new Form1();
frmnew.ShowDialog();
回答by Nisha
You can also use MDIParent-child form. Set the child form's parent as MDI Parent
您也可以使用 MDIParent-child 形式。将子窗体的父级设置为 MDI 父级
Eg
例如
child.MdiParent = parentForm;
child.Show();
In this case just 1 form will be shown and the child forms will come inside the parent. Hope this helps
在这种情况下,只会显示 1 个表单,子表单将进入父表单。希望这可以帮助
回答by Rizwan Ansari
Its simple, use
它的简单,使用
Form.ShowDialog();
Instead
反而
Form.Show();
While using Form.ShowDialog()
you cannot interact with the parent form until it closes.
使用时,Form.ShowDialog()
您无法与父表单交互,直到它关闭。
回答by Don Thomas Boyle
@Melodia
@Melodia
Sorry for this is not C# code but this is what you would want, besides translating this should be easy.
抱歉,这不是 C# 代码,但这正是您想要的,而且翻译这应该很容易。
FORM1
表格1
Private Sub Form1_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
Me.Focus()
Me.Enabled = True
Form2.Enabled = False
End Sub
Private Sub Form1_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
Form2.Enabled = True
Form2.Focus()
End Sub
FORM2
表格2
Private Sub Form2_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
Me.Focus()
Me.Enabled = True
Form1.Enabled = False
End Sub
Private Sub Form2_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
Form1.Enabled = True
Form1.Focus()
End Sub
Hope this helps
希望这可以帮助