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:
Maximize the window (only partial screen show to avoid a big image):
And restore it back (notice the scroll bar and the layout position on client area):
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?