首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Winforms:如何使CenterParent与SubForms和ModalForms一起工作?

C# Winforms:如何使CenterParent与SubForms和ModalForms一起工作?
EN

Stack Overflow用户
提问于 2022-03-15 11:41:30
回答 2查看 53关注 0票数 0

我有一个MainForm,在面板中有一个子表单SubLevel1。在SubLevel1中,我有另一个子表单SubLevel2,它在SubLevel1内的一个面板中打开。现在,在SubLevel2,我将与ShowDialog()一起打开一个ModalForm,我想把它放在SubLevel2 resp之上。MainForm (通过SubLevel1包含SubLevel2 )。

CenterParent只是不能工作,并且无法获得正确的(相对)位置来正确定位ModalForm

  1. I无法将SubLevel2设置为ModalFormParent:“无法将顶级控件添加到控件”错误弹出。因此,null.

Parent of ModalForm始终是

  1. 当我将SubLevel2设置为Owner of ModalForm时,它的位置0,0始终不能用于定位

  1. 当我使用ownerLocation = modalForm.Owner.PointToClient(Point.Empty)时,位置不正确。

  1. 当我使用ownerLocation = modalForm.Owner.PointToScreen(Point.Empty)时,位置不正确。

在创建SubForms时,我这样做:

代码语言:javascript
复制
  _FormSub = new FormSub() {
    TopLevel = false,
    TopMost  = false
  };
  panelSub.Controls.Add(_FormSub);
  _FormSub.Show();

ModalForm代码中创建SubForm2时,我这样做:

代码语言:javascript
复制
  formModal = new formModal() {
    Owner = this
  };
  formModal.ShowDialog();

我需要改变什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-15 12:40:55

通过尝试和错误找到解决方案。

创建ModalForm时,需要将创建的ModalForm的Owner设置为调用表单的TopLevelControl

代码语言:javascript
复制
FormModal formModal = new FormModal() { Owner = this.TopLevelControl as Form };

这也适用于从现有的ModalForm创建另一个ModalForm (然后,现有的ModalForm本身就是TopLevelControl)。

因此,您可以确保所创建的Owner的ModalForm始终具有正确的屏幕位置,通过该可以以编程方式设置ModalForm在其FormLoad()方法中的中心位置:

代码语言:javascript
复制
int centerX   = (this.Owner != null) ? this.Owner.Location.X + (this.Owner.Width  / 2) : screen.WorkingArea.Width  / 2;
int centerY   = (this.Owner != null) ? this.Owner.Location.Y + (this.Owner.Height / 2) : screen.WorkingArea.Height / 2;
int locationX = (centerX - (this.Width  / 2) > 0) ? centerX - (this.Width  / 2) : 0;
int locationY = (centerY - (this.Height / 2) > 0) ? centerY - (this.Height / 2) : 0;

if (locationX > screen.WorkingArea.Width)  { locationX = screen.WorkingArea.Width  - this.Width;  }
if (locationY > screen.WorkingArea.Height) { locationY = screen.WorkingArea.Height - this.Height; }

this.Location      = new Point(locationX, locationY);
form.StartPosition = FormStartPosition.Manual;

这可以确保在没有使用ParentCenterParent的情况下打开在调用表单的中央,或者-(如果没有设置Owner )在不使用CenterScreen的情况下以屏幕为中心。

它还确保ModalForm将始终在实际屏幕的边界内打开。

票数 1
EN

Stack Overflow用户

发布于 2022-03-15 12:45:44

我有些东西可以帮你:

代码语言:javascript
复制
private Form Activeform = null;

private void OpenChildForm(Form childForm)
{
    if (Activeform != null)
    {
        Activeform.Close();
    }

    Activeform = childForm;
    childForm.Visible = false;
    childForm.BackColor = Color.FromArgb(32, 30, 45);
    childForm.TopLevel = false;
    childForm.FormBorderStyle = FormBorderStyle.None;
    childForm.Dock = DockStyle.Fill;
    panel1.Controls.Add(childForm);
    panel1.Tag = childForm;
    childForm.BringToFront();
    childForm.Show();
}

这样就可以使方法可重用。你会称之为它的例子

代码语言:javascript
复制
OpenChildForm(new Form1());
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71481638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档