C# 以编程方式定位面板以填充父窗体的三分之一 (WinForm)

C# programmatically position panel to fill one third of the parent form (WinForm)

我有一个带有面板和 RichTextBox 的表单。 Panel 为 Dock.Top,RTB 为 Dock.Bottom。初始化后。 我希望定位面板,使其占据表格顶部的 1/3,RTB 占据表格的剩余底部。

但是,下面显示的代码不起作用。

 public frmMain()
        {
            InitializeComponent();
            panel1.Top = 0;
            panel1.Width = this.Width;
            panel1.Height = this.Height / 3;
            ConOut.Top = panel1.Height + 10;
            ConOut.Width = this.Width;

        }

问题 1) 上面的代码有什么问题以至于它没有正确定位窗体的控件?

我猜你只需要添加 Left 个位置。

panel1.Left = 0; // Sets top left corner position

可以解决问题的完整代码。

        InitializeComponent();
        panel1.Top = 0;
        panel1.Left = 0;
        panel1.Width = this.Width;
        panel1.Height = this.Height / 3;

        ConOut.Left = 0;
        ConOut.Top = this.Height / 3;
        ConOut.Width = this.Width;
        ConOut.Height = 2*this.Height / 3;