FormStartPosition.CenterParent不起作用 [英] FormStartPosition.CenterParent does not work

查看:2880
本文介绍了FormStartPosition.CenterParent不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,只有第二种方法对我的作品(.NET 4.0)。 FormStartPosition.CenterParent 不超过中心其父子窗体。
为什么



来源:?的这太问题

 使用系统; 
使用System.Drawing中;使用System.Windows.Forms的
;

类节目
{
私有静态表格F1;

公共静态无效的主要()
{
F1 =新表(){宽度= 640,高度= 480};
f1.MouseClick + = f1_MouseClick;
Application.Run(F1);
}

静态无效f1_MouseClick(对象发件人,MouseEventArgs E)
{
组成F2 =新表(){宽度= 400,高度= 300};
开关(e.Button)
{
情况下MouseButtons.Left:
{
//第一个方法
f2.StartPosition = FormStartPosition.CenterParent;
中断;
}
情况下MouseButtons.Right:
{
//第2个方法
f2.StartPosition = FormStartPosition.Manual;
f2.Location =新的点(
f1.Location.X +(f1.Width - f2.Width)/ 2,
f1.Location.Y +(f1.Height - F2。身高)/ 2
);
中断;
}
}
f2.Show(F1);
}
}


解决方案

这是因为你没有告诉F2谁是它的母公司是。



如果这是一个MDI应用程序,然后F2应该有的MdiParent 设置为 F1

 表F2 =新表( ){宽度= 400,高度= 300}; 
f2.StartPosition = FormStartPosition.CenterParent;
f2.MdiParent = F1;
f2.Show();

如果这不是一个MDI应用程序,那么你就需要调用的ShowDialog 使用方法 F1 作为参数。<​​/ p>

 表单F2 =新表(){宽度= 400,高度= 300}; 
f2.StartPosition = FormStartPosition.CenterParent;
f2.Show(F1);



注意CenterParent不与显示因为没有办法设置家长,因此,如果将ShowDialog的是不恰当的,手动的方法是唯一可行的。


In the following code, only the second method works for me (.NET 4.0). FormStartPosition.CenterParent does not center the child form over its parent. Why?

Source: this SO question

using System;
using System.Drawing;
using System.Windows.Forms;

class Program
{
  private static Form f1;

  public static void Main()
  {
    f1 = new Form() { Width = 640, Height = 480 };
    f1.MouseClick += f1_MouseClick; 
    Application.Run(f1);
  }

  static void f1_MouseClick(object sender, MouseEventArgs e)
  {
    Form f2 = new Form() { Width = 400, Height = 300 };
    switch (e.Button)
    {
      case MouseButtons.Left:
      {
        // 1st method
        f2.StartPosition = FormStartPosition.CenterParent;
        break;
      }
      case MouseButtons.Right:
      {
        // 2nd method
        f2.StartPosition = FormStartPosition.Manual;
        f2.Location = new Point(
          f1.Location.X + (f1.Width - f2.Width) / 2, 
          f1.Location.Y + (f1.Height - f2.Height) / 2
        );
        break;
      }
    }
    f2.Show(f1); 
  }
}

解决方案

This is because you are not telling f2 who its Parent is.

If this is an MDI application, then f2 should have its MdiParent set to f1.

Form f2 = new Form() { Width = 400, Height = 300 };
f2.StartPosition = FormStartPosition.CenterParent;
f2.MdiParent = f1;
f2.Show();

If this is not an MDI application, then you need to call the ShowDialog method using f1 as the parameter.

Form f2 = new Form() { Width = 400, Height = 300 };
f2.StartPosition = FormStartPosition.CenterParent;
f2.Show(f1);

Note that CenterParent does not work correctly with Show since there is no way to set the Parent, so if ShowDialog will is not appropriate, the manual approach is the only viable one.

这篇关于FormStartPosition.CenterParent不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆