Search code examples
c#winformssplitcontainer

C# splitContainer1 resize child control


I have a splitContainer. I want to resize the form inside the splitContaner's panel to scale with when I move the splitter as below. But my form doesn't get redrawn. Any suggestion, thank so much!

    private void splitContainer1_SplitterMoved(System.Object sender, System.Windows.Forms.SplitterEventArgs e)
    {
        // Define what happens when the splitter is no longer moving.
        Cursor.Current = System.Windows.Forms.Cursors.Default;
        statictisTableDisplayForm1.ClientSize = new Size(statictisTableDisplayForm1.Width, splitContainer1.SplitterDistance);
        statictisTableDisplayForm1.Invalidate();
        statictisTableDisplayForm1.Refresh();
        Refresh();
    }

Solution

    1. Form supposed to be a top-level control which represents a window of your application. You should not embed forms as controls into other forms (well, unless there is no other option).
    2. Usually, you should not resize and/or move controls manually. There are several layout options which allow automatic resizing of controls when the size of their container changes: Anchor, Dock.

    So better create a UserControl which will contain controls and logic of your StatictisTableDisplayForm and place it to SplitContainer panel with Dock set to Fill. That will automatically resize user control when you move the splitter.

    Note: if you have to use StatictisTableDisplayForm on it's own too, then just place same user control to this form.