Search code examples
c#winformslayoutposition

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


I have a form with a Panel and a RichTextBox. The Panel is Dock.Top and the RTB is Dock.Bottom. After initialization. I wish to position the panel so it takes up the top 1/3 of the form with the RTB taking up the remaining bottom of the form.

However, the code presented below does not work.

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

        }

Question 1) What is wrong with the above code so that it does not position the form's controls properly?


Solution

  • You simply need to add Left position I guess.

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

    Complete code that could solve the problem.

            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;