To make it short: I have a ListView and I want to scale it dynamically oriented to a container which the ListView is inside of. Am I too stupid to read or is there no property for this? And if there is none, does somebody have an idea how to?
-
Anchors dont work for some reason, it says something like "Wrong Value" in a MessageBox (not called by me) if i compile. Dock would hide my buttons. (After that box, the anchor is removed again in VS – Horius Aug 15 '14 at 14:47
-
Don't lump it all in one Panel. Look at TableLayout and/or SplitPanel. They were made for this. – H H Aug 15 '14 at 14:48
-
Related: http://stackoverflow.com/questions/10461876/automatically-scaling-child-controls-when-a-form-is-maximized/10461917#10461917 and http://stackoverflow.com/questions/23259261/automatically-adjusting-winform-and-controls-to-screen-size/23259523#23259523 – Cody Gray - on strike Aug 15 '14 at 15:49
3 Answers
What you have to do is set the "anchor" on the control. You set the anchor points to correspond to how you want the control to change size.
If you want the control to fill the entire space, simply set the "Dock" To center. You can also set the dock to a few other things. See Here

- 6,798
- 1
- 24
- 51
-
Thanks for the detailed answer with the screen, but for some reason it wont work,it says something like "wrong value" in a messagebox not called by me – Horius Aug 15 '14 at 14:45
-
Not really to sure what your talking about here. If you need more help, provide the exact error message that is being shown or What behavior your experiencing. Also, sounds like you have a custom error handler that is showing that message box (not a win forms issue) – iamkrillin Aug 15 '14 at 14:47
-
Well, it comes from VS because after that error the anchor is resetted in the propertys. The error says "Invalid Propertyvalue" - screens wouldnt tell you much, my studio is on german :p – Horius Aug 15 '14 at 14:49
-
-
I tried to set the anchor in each direction, but every time i select 2 opposite sites, it will give me that error – Horius Aug 15 '14 at 14:53
-
Are you using the GUI provided by visual studio, typing it in the box on the properties panel, or doing it in code? – iamkrillin Aug 15 '14 at 14:54
-
I am using the visual studio 2013 professional propertys panel. The same you used for your screenshot – Horius Aug 15 '14 at 14:55
-
Sounds like it might be an issue with visual studio actually, see here: http://stackoverflow.com/questions/9609316/property-value-is-not-valid-why-is-visual-studio-not-letting-me-assign-a-pict – iamkrillin Aug 15 '14 at 14:57
-
Click on "Details" in that window why the value is invalid. Probably you tried to change it while the program is running or if the file is write-protected (both causes this error). – Ray Aug 15 '14 at 16:09
You can also use the Resize event on your form like this:
private void Form1_Resize(object sender, EventArgs e)
{
int SIDEPAD = 10;
listBox1.Width = Width / 2;
listBox1.Height = Height / 2;
listBox1.Top = (Height - listBox1.Height - button1.Height) / 4;
listBox1.Left = (Width - listBox1.Width - SIDEPAD) / 2;
button1.Width = Width / 2;
button1.Top = ((Height - listBox1.Height - button1.Height) / 4) * 2 + listBox1.Height;
button1.Left = (Width - listBox1.Width - SIDEPAD) / 2;
}
This will keep a button and listbox equally spaced... it is kind of annoying to program, but it will make sure everything always looks nice. (Sidepad is the width of the border of the form; I think it may actually be 8... it looks fine to me though.)

- 1,307
- 12
- 19
-
Width is form width, you could just make it the width of the container the control is inside of. – Taugenichts Aug 15 '14 at 15:57
The solution is to put your controls into a TableLayoutPanel
and set the Dock
of your ListView
to Fill
. Then, set the Dock
of TableLayoutPanel
to Fill
as well so that it will adjust according to its parent form.

- 4,631
- 24
- 40