1

I'm using Windows Forms and I'd like to write some code to change the layout of each of the controls on the form whenever anything gets scrolled or resized. I assume there must be a standard way of doing this before a form paint is done.

EDIT: There is a DataGridView on the form. I want to change the layout whenever a column width is changed or the horizontal scroll bar is moved.

Paul Matthews
  • 2,164
  • 5
  • 20
  • 29
  • 2
    you are aware that controls can handle layout considering scrolling and size of parent container (on controls Anchor, Dock and AutoScroll on panel)? – Rafal Nov 02 '12 at 08:07

3 Answers3

1

Override those two methods in your form:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
}

protected override void OnScroll(ScrollEventArgs se)
{
    base.OnScroll(se);
}
LightStriker
  • 19,738
  • 3
  • 23
  • 27
  • So I would also have to do that for every other control in the form that can be resized or scrolled as well? – Paul Matthews Nov 02 '12 at 08:09
  • @PaulMatthews: Lot of control can automatically resize themself if you anchor them. Or the form can handle their resize too, you don't have to do it per control. – LightStriker Nov 02 '12 at 08:14
0

You don't need to create any positioning and resizing code if you place your objects inside a TableLayoutPanel. This control acts pretty much as HTML table, well not quite exactly so.

Take a look at the following link how to use TableLayoutPanel:

TableLayoutPanel Class (System.Windows.Forms)

Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
0

whenever anything gets scrolled or resized

Please be precise.


What do you expect to change size?
Where does the scrolling occur? (in the form, in list box or other)

If you want to change layout in form resize, you can do it in Form.Resize event handler.

For scrolling in form, use ScrollEventArgs

Take a look at these questions as well.

Scrolling

Form Resize event - MSDN

Community
  • 1
  • 1
CRoshanLG
  • 498
  • 1
  • 8
  • 20