SplitContainer Panel Resize Issue

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

    I am wondering if the issue is related to this problem here, but I am not prepared to create a new "MySplitContainer" to try and solve this problem.

    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?)

  • 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.


    Exactly the same problem, below code worked for me:

  • Surround splitContainer in a panel "tableBorder"
  • On tableBorder

    Dock = DockStyle.Fill;
    

    On split Container, (no anchoring)

    Dock = DockStyle.None;
    
  • 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

    链接地址: http://www.djcxy.com/p/62002.html

    上一篇: DateTimePicker UserControl上的动态文本颜色

    下一篇: SplitContainer面板调整大小问题