locked
WinForm - Multiple Panels for Treeview, DataGridView and RichTextBox RRS feed

  • Question

  • Just wondering if it is a correct approach to place two SplitContainers on a form to make multiple panels.

    The first one (named as SplitContainer1) is just placed on a form, and the second one is placed on the SplitContainer1's Panel2.

    Please see the picture below,  

    What I want to accomplish is to place a Treeview on the left hand side, and a DataGridView on the top-right and a RichTextBox on the bottom-right.  

    I am just wondering if this is a correct approach for doing that?  Please advise, and thanks 

    Tuesday, April 28, 2020 3:58 AM

Answers

  • Hi iHandler,
    when you place the splitContainer2 on the splitContainer1's panel2, splitContainer2 is above splitContainer1's panel2 by default.
    So you can drag controls to different panels. To avoid manual mistakes, I suggest you can use Control.Add() method to add controls in the panel.
    Here is a simple code example:

    private void Form1_Load(object sender, EventArgs e)
    {
        splitContainer1.Panel1.Controls.Add(treeView1);
        splitContainer2.Panel1.Controls.Add(dataGridView1);
        splitContainer2.Panel2.Controls.Add(richTextBox1);
       // Create the horizontal splitter.     
        splitContainer2.Orientation =Orientation.Horizontal;
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 28, 2020 9:27 AM
  • Hi iHandler,
    If you don't want the overlapping layout, you can directly use the panel control to split the form. And then add different controls to different panels.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by iHandler Thursday, April 30, 2020 4:53 AM
    Wednesday, April 29, 2020 9:17 AM

All replies

  • Hi iHandler,
    when you place the splitContainer2 on the splitContainer1's panel2, splitContainer2 is above splitContainer1's panel2 by default.
    So you can drag controls to different panels. To avoid manual mistakes, I suggest you can use Control.Add() method to add controls in the panel.
    Here is a simple code example:

    private void Form1_Load(object sender, EventArgs e)
    {
        splitContainer1.Panel1.Controls.Add(treeView1);
        splitContainer2.Panel1.Controls.Add(dataGridView1);
        splitContainer2.Panel2.Controls.Add(richTextBox1);
       // Create the horizontal splitter.     
        splitContainer2.Orientation =Orientation.Horizontal;
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 28, 2020 9:27 AM
  • Thanks.

    For this layout design (I mean my original idea), I still feel a bit wired for using two splitContainers to create this layout.

    Honestly, I don't feel comfortable to have an overlapping layout. :-(

    Now, I don't quite to have a direction to go.

    Tuesday, April 28, 2020 10:17 AM
  • Hi iHandler,
    If you don't want the overlapping layout, you can directly use the panel control to split the form. And then add different controls to different panels.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by iHandler Thursday, April 30, 2020 4:53 AM
    Wednesday, April 29, 2020 9:17 AM
  • Hi iHandler,
    If you don't want the overlapping layout, you can directly use the panel control to split the form. And then add different controls to different panels.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Yes, I did the same layout with different controls

    Approach 1 - Use two splitContainers to divide the layout

    Approach 2 - Use one splitContainer and one splitter to divide the layout 

    I found that using Approach 1 or Approach 2 can achieve the same layout but using Approach 2 got a problem.  If the DataGridView contains many rows (say over 300 rows), the screen becomes "Flickering" when I click on the "Show Panel / Hide Panel" toggle button for the reading panel (i.e. the RichTextBox)

    In fact, using Approach 1 is much more easier, pretty smooth to build up the layout.  Also, I found that Microsoft suggests to use SplitContainers, rather than Splitter control. 

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/splitcontainer-control-windows-forms

    Anyway, Daniel, thanks for your suggestion.  ~

    Thursday, April 30, 2020 4:52 AM