一起移动两个表格而不改变两个表格的位置(后面或前面) [英] Moving two forms together without changing the positions (behind or front) of the two forms
问题描述
我对 C# 很陌生.最近遇到一个问题,就是想把两个不同的表格一起移动.
I am quite new to C#. Recently, I got stuck on a problem which is that I want to move two different forms together.
更详细,
一个大表单(即作为父窗口的东西)弹出一个小表单.
One big form (i.e. something as parent window) popups a small form.
我要做的是通过移动父窗口来同时移动这两个窗体.
What I want to do is to move these two forms at the same time by moving the parent window.
而且,现在我正在通过 Form 的Show()"方法创建子(小)表单.问题是,如果我点击父表单,一个小表单会在父表单后面(即更大的表单).
And, now I am creating the children (small) form by "Show()" method of Form. The problem is that if I click the parent form, a small form goes behind the parent form (i.e. bigger form).
我知道这是应该发生的.但是,我想通过移动较大的表格同时保持小表格在前面来移动这两个表格.
I know this is what should happen. However, I want to move these two forms by moving the bigger form while keeping the small form is front.
我也考虑过使用ShowDialog()".但是,这阻止了我移动更大的父母.我无法触摸父窗口.
I also considered using "ShowDialog()". But, this prevent me from moving the bigger parent. I cannot event touch the parent window.
推荐答案
将子窗体的 Owner 属性设置为父窗体:
Set the child form's Owner property to the parent form:
this.childForm = new ChildFormClass();
child.Owner = this;
viewForm.ShowDialog();
//Can also be called like this instead of setting Owner property:
//viewForm.ShowDialog(this);
参考:http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx
这样,子窗体将永远不会显示在父窗体后面.
This way, the child form will never be shown behind the parent.
要将两个表单一起移动,只需处理父表单的 Move 事件:
To move both forms together, simply handle the parent's form Move event:
private void Form1_Move(object sender, EventArgs e)
{
Point p = this.PointToScreen(new Point(this.ClientRectangle.X, this.ClientRectangle.Y));
this.childForm.Location = p; //childForm needs to be a class member
}
干杯
这篇关于一起移动两个表格而不改变两个表格的位置(后面或前面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!