public partial class MainForm : Form
{
public MainForm()
{
var listView = new ListView();
listView.Dock = DockStyle.Top;
listView.Height = 200;
var bottomPanel = new Panel();
bottomPanel.Dock = DockStyle.Fill;
// outer panels
var group = new Panel();
var innerGroup = new Panel();
var innerInnerGroup = new Panel();
// if innerGroup has Anchor = Left | Right | Top | Botton instead of Dock = Fill,
// scroll position is not reset
group.Dock = DockStyle.Fill;
innerGroup.Dock = DockStyle.Fill;
innerInnerGroup.Dock = DockStyle.Fill;
Controls.Add(group);
group.Controls.Add(innerGroup);
innerGroup.Controls.Add(innerInnerGroup);
innerInnerGroup.Controls.Add(bottomPanel);
innerInnerGroup.Controls.Add(listView);
// populate listView
listView.Columns.Add(new ColumnHeader("Column"));
for (int i = 0; i < 100; i++)
{
listView.Items.Add("Item " + i);
}
// if this little fella has Dock property set to Fill, scroll position is not reset
var troublemaker = new Panel { BackColor = Color.Blue };
var blankPanel = new Panel { Dock = DockStyle.Fill };
listView.SelectedIndexChanged += (s, e) =>
{
if (listView.SelectedItems.Count == 1)
{
bottomPanel.Controls.Clear();
bottomPanel.Controls.Add(troublemaker);
}
else
{
bottomPanel.Controls.Clear();
bottomPanel.Controls.Add(blankPanel);
}
};
}
}