Question

I'm still new to Microsoft Visual Studio and C#, but to make a windows forms application a certain size in the designer mode in Visual Studio 2013, you just drag the ends, and it automatically makes it the way you sized it. This is handy, but it has its disadvantages. One of the main ones being that when you maximize the windows forms app, all the buttons, and stuff that you built, doesn't change size to fit the screen. Is there a special option or something I'm missing to make all the stuff inside the win forms app change size with the way the app is resized by the user?

Était-ce utile?

La solution

You should be entering all your Controls in a TableLayoutPanel. You can control how the row and column move. One catch, only one control per cell, so sometimes you have to either nest another TableLayoutPanel or dock a panel control back into a cell to get all the things you need back on screen. On the left is my form as designed, on the right my re-sized one while running in debug mode. enter image description here

Deep Dive
1 TablelayoutPanel of 4 columns & 5 rows, 1 groupbox, 2 buttons
Column 0 = Fixed at 20px
Column 1 = 100%
Column 2 = Auto-Sized to Control
Column 3 = Fixed at 20px

Row 0 = Fixed at 20px
Row 1 = 100%
Row 2 = Fixed at 20px
Row 3 = Auto-Sized to Control
Row 2 = Fixed at 20px

Button1 in Column 1 at Row 3, Anchored Right
Button2 in Column 2 at Row 3, Anchored Right
Groupbox in Column 1 at Row 1, colored for visibility, dock = Fill, ColSpan = 2
TableLayoutPanel Dock=Fill

Autres conseils

You should decide on which control resizes with the window. Choose only one if possible and probably the center one which would also be the output. Then arrange the input conrols around it (like buttons on upper or lower edges) and anchor them to the edges (Anchor property). Don't resize the input controls, users don't like that. On the other hand, the more real estate you can give to the output the better. If you're not certain about what I'm talking about just resize any common desktop app and observe how inputs stay anchored and don't change size while the center piece gets all the benefits of resizing.

Edit: Also, you can dock panels on south, north, east and west sections of the form window. You should really try and spend an hour or so fiddling around. You can get the hang of what is best only by trying, because it is very much bound to usability and not a pure technical problem.

Just one reminder: don't resize controls while the user is resizing the window. Whatever you do to the controls first suspend layout by invoking SuspendLayout() on the form. At the end, invoke ResumeLayout().

if you set the default window state to maximized, and put this code in your form load:

Console.WriteLine(this.Size);

you can see at the console your maximized window size. this way you can resize your form in the designer so that you can add and move controles easily!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top