Paul Matthews
Paul Matthews

Reputation: 2214

Where should I put the code to resize/reposition controls on a form in C#?

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.

Upvotes: 1

Views: 131

Answers (3)

Gregor Primar
Gregor Primar

Reputation: 6805

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)

Upvotes: 0

CRoshanLG
CRoshanLG

Reputation: 498

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

Upvotes: 0

LightStriker
LightStriker

Reputation: 21034

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);
}

Upvotes: 1

Related Questions