Problem with resizing SplitContainer panel

a common problem

C # WinForms.Net 4.0 Application.

I have a SplitContainer that takes up most of the form, it is anchored in all directions, so it resizes along with the form. The left panel (Panel1) has a simple menu, there are no problems. The right panel (Panel2) is more complex and contains several nested tab controls (with a large number of controls) - it is very complex, but does not change.

The problem is that resizing the form does not work so well. In fact, if you resize by slowly dragging the edges, then this works fine, but drag quickly or use the "restore" button (in the upper right corner of the form), then the problem arises.


My management hierarchy

The following is a simple example of my control hierarchy, this is a definitely truncated version, but it highlights a nested tab control that can help with replication:

  • the form
    • Split container (anchor: top, left, bottom, right)
      • SC Panel1 (Minimum Width: 300)
        • TreeViewControl (forget what it's called)
      • SC Panel2
        • Panel (anchor: top, left, bottom, right)
          • Tab management (snap: top, left, bottom, right)
            • Managing tabs with a large number of pages that go beyond the screen and require that the navigation buttons appear in the upper right corner (snap: top, left, bottom, right)

Debug Details

After some debugging, it turns out that in fact, Panel2 (a child of a split container) is not the SplitContainer properly, but the SplitContainer itself changes the SplitContainer normally.

Here are the debug values โ€‹โ€‹that show this ...

Full width of the form, before resizing:

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

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

After resizing, where the problem occurs:

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

As you can see, splitContainerMain.Width is optional, but splitContainerMain.Panel2.Width and its children splitContainerMain.Panel2.Width did not.

NOTE Please remember that the width is updated correctly if I slowly resize the form manually - this is not a problem if I incorrectly configure any anchors.


My efforts so far

I tried to use various form resize events and try to set the width manually, but to no avail. I think I would like to try setting the value of Panel2.Width from some event.


What i'm looking for

  • Is there anyway to force splitContainerMain.Panel2.Width to splitContainerMain.Panel2.Width correctly when splitContainerMain is splitContainerMain ?
  • Also, how can I calculate what Panel2.Width should be? And how can I set this value from the Form.Resize event? (or another event?)
+8
source share
4 answers

From what I see, u should set the binding to none for the controls that create the problem, including the splitcontainer panels.

I would also suggest using the dock fill property to make the best use of separation containers.

If you need further help, please provide the designer file so that it can look better.

0
source

Although this question has been around for 6 years, I decided to answer it because I was in the same situation as the first post. Unfortunately, the orientation was not specified. So, my answer will concern those that have a horizontal orientation.

Please translate to C #, as this code is written 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 
0
source

Thus, with each Change event, you create a new thread, this thread will wait 100 ms, and then make changes ??? this is silly. You can create a stream in the constructor, then call Start () on your stream, which may have the following:

 private void resizeMe() { this.BeginInvoke((Action)() => { splitContainer.Height = tableBorder.Height; splitContainer.Width = tableBorder.Width; } } 
-1
source

Exactly the same problem, below code worked for me:

  • Surround splitContainer in the tableBorder panel

Table Border

 Dock = DockStyle.Fill; 

In the Separation Container section (no binding)

 Dock = DockStyle.None; 
  1. In the 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 slight lag, but it works

-3
source

Source: https://habr.com/ru/post/951115/


All Articles