4

I have a user control that display a passenger car seating layout.

It simple draws several "SeatControl" in a matrix like fashion, depending on the passenger car size.

For better viewing, the main control resizes the "SeatControl" for fitting all space available, that means that the SeatControls will get bigger or smaller depending on the space available.

This works perfect.

But, when the client area gets too small, we avoid shrinking the controls too much, or they became deformed and impossible to read.

In this case we turn on auto scroll so the user can scroll around to see the entire layout.

But, if we start with a small screen (with scroll bar), maximize it (the scroll bar will hide and the seat controls increase size) and restore the window size back (scroll will come back and seat controls will shrink to minimum size), the scroll gets lost.

To make it clear, the same operation in images: small layout with vertical scroll bar

Maximize the window (only partial screen show to avoid a big image):

partial full screen, notice that scroll has gone as expected

And restore it back (notice the scroll bar and the layout position on client area): but on scroll bar

This resize is controlled by the code below:

private void FixSizes()
    {            
        if (mModel == null)
            return;

        this.SuspendLayout();            

        Size clientSize = this.ClientSize;
        Size minimumSize = new Size(SeatUserControl.MinimumDescentSize.Width, SeatUserControl.MinimumDescentSize.Height);

        //Here we try to find the best size for the seat user control to fit all the client area                      
        Size controlSize = new Size(
            Math.Max(clientSize.Width / mModel.Length, minimumSize.Width),
            Math.Max(clientSize.Height / mModel.Width, minimumSize.Height)
        );

        AutoScrollMinSize = new Size(controlSize.Width * mModel.Length, controlSize.Height * mModel.Width);
        this.SetDisplayRectLocation(0, 0);

        for (int row = 0; row < mModel.Width; ++row)
        {
            for (int col = 0; col < mModel.Length; ++col)
            {
                Control control = this.Controls[(row * mModel.Length) + col];

                control.Location = new Point(col * controlSize.Width, row * controlSize.Height);
                control.Size = controlSize;
            }
        }            

        this.ResumeLayout();
    }

And this method is simple called by the OnClientSizeChanged method:

protected override void OnClientSizeChanged(EventArgs e)
    {
        base.OnClientSizeChanged(e);

        this.FixSizes();
    }

I was able to determine that if I keep the SeatControl on a fixed size, the problem goes away, but the output is not so good, as we prefer the SeatControl to use the maximum space available.

So it looks like I am missing or forgetting to do something with autoscroll settings so it does not get lost. Any ideas?

KreepN
  • 8,528
  • 1
  • 40
  • 58
bcsanches
  • 2,362
  • 21
  • 32
  • Did you try with Dock setting of the panel to stretch across, in case you are using a panel for seat layout? – Siva Gopal Jul 13 '12 at 13:42
  • @SivaGopal: I did not, the SeatControls are inserted direct on the UserControl. But if I use the dock I will lost the scroll, right? – bcsanches Jul 13 '12 at 13:45
  • 1
    Assuming you are using a parent container of some sort, such as a panel, or something that would restrict the maximized size of the SeatControl, use dock "Fill" (the center one). This will auto adjust the scrollbar for you in regards to the control. – KreepN Jul 13 '12 at 14:11
  • 1
    @KreepN the User Control that holds all the SeatsControls was simple keeping the seats on itself. I added now a new panel with Dock set to "Fill" and filled this panel with the seat controls. It appears to be working nicely! Thanks! – bcsanches Jul 13 '12 at 14:56
  • Ported my comment to an answer. For future reference, utilizing tablelayoutpanels and split containers (with dock fill) can help maintain an aesthetically pleasing application. – KreepN Jul 13 '12 at 15:00
  • 1
    @KreeN my first design was with tablelayout, but I was not able to make it work ok with controls being added/removed and resized all the time. Manually controlling the controls FOR THIS case resulted in a much simpler and surprising, fast code. Thanks. – bcsanches Jul 13 '12 at 15:02

1 Answers1

2

Solution from comments:

Assuming you are using a parent container of some sort, such as a panel, or something that would restrict the maximized size of the SeatControl, use dock "Fill" (the center one). This will auto adjust the scrollbar for you in regards to the control.

For future readers, the result would look like this when using a Dock: Fill property:

enter image description here

Note the scrollbar that was auto-generated due the gridview control being larger than its parent container.

KreepN
  • 8,528
  • 1
  • 40
  • 58