SplitContainer Panel Resize Issue
Asked Answered
S

4

12

The General Problem

The application is C# WinForms .Net 4.0.

I have a SplitContainer that takes up most of the form, it is set to Anchor in all directions so it re-sizes along with the form. The left panel (Panel1) has a simple menu, no problems here. The right panel (Panel2) is more complex and contains a number of nested tab controls (with lots of controls) - it is painfully complex, but it's not changing.

The problem is that re-sizing the form doesn't work so well. In fact, if you resize by dragging the edges slowly then it works ok, but drag quickly or use the "restore" button (top-right of form) then the issue occurs.


My Control Hierarchy

The following is a simple example of my control hierarchy, its definitely a cut down version but does highlight the nested tab control which may help with replication:

  • Form
    • Split Container (anchor: top, left, bottom, right)
      • SC Panel1 (min width: 300)
        • TreeViewControl (forget what it is called)
      • SC Panel2
        • Panel (anchor: top, left, bottom, right)
          • Tab Control (anchor: top, left, bottom, right)
            • Tab Control w/ lots of pages that overflow screen and require the navigation buttons to show in top right corner (anchor: top, left, bottom, right)

Debug Details

After some debugging it appears that it is in fact Panel2 (a child of the split container) that doesn't resize properly, and the actual SplitContainer itself resizes fine.

Here are the debug values that show this...

Full width form, before resize:

splitContainerMain.Width: 1479
splitContainerMain.Panel2.Width: 1206
panelCenter.Width: 1203
tabControlMain.Width: 1215

All as expected, splitContainerMain.Panel2.Width is smaller than splitContainerMain.Width.

After resize where the issue occurs:

splitContainerMain.Width: 815
splitContainerMain.Panel2.Width: 1206
panelCenter.Width: 1203
tabControlMain.Width: 1215

As can be seen, the splitContainerMain.Width has resized as desired, but the splitContainerMain.Panel2.Width and subsequently its children have not.

NOTE: Please remember, the width updates correctly if I manually resize the form slowly - this is not a problem with me not correctly setting any anchors.


My Efforts So Far

What I have tried to do is use various Form resize events and try to set the widths manually, but to no avail. I think what I would like to try is to set the Panel2.Width value from within an event of some sort.


What I Am Looking For

  • Is there anyway to force splitContainerMain.Panel2.Width to resize correctly when the splitContainerMain size changes?
  • Alternatively, how can I calculate what the Panel2.Width should be? And how can I set that value from the Form.Resize event? (or another event?)
Sentiment answered 6/8, 2013 at 11:31 Comment(9)
I've tried to put together a form to test, but I have yet to see bad behavior as you've described. If you can provide some more layout details, it would help. But otherwise, I'm not having much luck reproducing your results.Schmuck
@DonBoitnott: I have added some details about my layout, I know this isn't going to be easy to replicate but perhaps it will do something. I will see if I can replicate in a new winforms app tooSentiment
What is panelCenter? And how are you resizing...in code?...simply dragging the form edge?Schmuck
@Sentiment I think Panel2 always has Dock = DockStyle.Fill (the Dock is hidden`) so your problem is very strange, I think you should post some screen shot instead of a lot of words.Perchance
@DonBoitnott: My question explains how I am resizing in the first section: by dragging, and by using the "restore" button on the form window. panelCenter is the panel directly added to Panel2 in the control hierarchy, and it contains the first tabcontrolSentiment
@KingKing: Yes my problem is very strange, but I cannot post any screenshots I am afraid. Imaging a panel with a width of 600 and it has a tab control inside with a width of 1000 - you would not see the right 400 pixels of the tabcontrol. That is what it looks like.Sentiment
Try setting min size of both Panels within the splitPanel to zero and set the FixedPanel property to Panel1.Seaport
The problem link to microsoft (in the "My Effords so Far" section is brokenAsher
@PedroPolonia: Thanks, I have removed that paragraph now as it makes no sense without a working linkSentiment
A
2

Though the question is about 6 years old, I opted to answer this because I was in the same situation as the opening post. Unfortunately, the orientation was not specified. So, my answer would address the ones with Horizontal orientation.

Please translate to C# as this code is in VB.

 Private Sub splitContainerMain_Resize(sender As Object, e As EventArgs) Handles splitContainerMain.Resize
    '/* This is a work around about panels being left out when SplitContainer is resized */
    Dim pnl1Height As Single = splitContainerMain.SplitterDistance '/* Get upper panel height */
    Dim pnl2Height As Single = splitContainerMain.Height - splitContainerMain.SplitterDistance '/* Get lower panel height */
    splitContainerMain.Panel1.SetBounds(0, 0, splitContainerMain.Width, pnl1Height) '/* Set Upper panel bounds */
    '/* Set lower panel bounds, with a top of upper panel height plus splitter width */
    splitContainerMain.Panel2.SetBounds(0, pnl1Height + splitContainerMain.SplitterWidth, splitContainerMain.Width, pnl2Height)
End Sub
Adamek answered 20/3, 2019 at 10:31 Comment(0)
D
0

From what I see u should set anchor to none for controls that are creating problem including splitcontainer pannels.

Also I would suggest to use dock fill property to best use the splitcontainers.

If need further help please provide designer file so can have a better look.

Dark answered 28/11, 2014 at 7:15 Comment(0)
T
-1

This is a translation of Zaldy Baguinon's answer from VB.Net to C# with a vertical orientation. I have made updates to adjust the splitter location when the right panel reaches its minimum size.

private void splitContainerMain_Resize(object sender, EventArgs e)
{
    // This is a work around about panels being left out when SplitContainer is resized
    var pnl2Width = splitContainerMain.Width - splitContainerMain.SplitterDistance; // Get right panel height
    if(pnl2Width < splitContainerMain.Panel2MinSize) 
    {
        pnl2Width = splitContainerMain.Panel2MinSize; // right panel can't be smaller than min size
        splitContainerMain.SplitterDistance = splitContainerMain.Width - splitContainerMain.Panel2MinSize; // alter splitter location
    }
    var pnl1Width = splitContainerMain.SplitterDistance;//  Get left panel width
    splitContainerMain.Panel1.SetBounds(0, 0, splitContainerMain.Height, pnl1Width); // Set left panel bounds
    // Set right panel bounds, with a Main of left panel width plus splitter width
    splitContainerMain.Panel2.SetBounds(pnl1Width + splitContainerMain.SplitterWidth, 0, pnl2Width, splitContainerMain.Height);
}
Toneme answered 20/12, 2023 at 10:18 Comment(0)
H
-3

Exactly the same problem, below code worked for me:

  1. Surround splitContainer in a panel "tableBorder"

On tableBorder

Dock = DockStyle.Fill;

On split Container, (no anchoring)

Dock = DockStyle.None;
  1. On tableBorder SizeChanged event

    private void tableBorder_SizeChanged(object sender, EventArgs e)
    {
        new Thread(() => { resizeMe(); }).Start();
    }
    
    private void resizeMe()
    {
        Thread.Sleep(100);
    
        this.BeginInvoke((Action)(() => {
    
            doIt();
    
        }));
    }
    
    private void doIt()
    {
        splitContainer.Height = tableBorder.Height;
        splitContainer.Width = tableBorder.Width;
    }
    

There is a small lag, but works

Hugibert answered 23/9, 2015 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.