views:

83

answers:

2

Hey there.

The program I'm working on at the moment uses C# WinForms (on the 2.0 .NET Framework, though that probably won't make a difference in this case). The design calls for the ability to scale a form and all of its controls up and down. I did this by looping through each of the controls, saving their original sizes and locations, and getting their ratio to the original form size to scale with. It looks a little something like this:

const int SCALE_W = 600;
const int SCALE_H = 500;

...

if (!listsBuilt){
    foreach (Control c in this.Controls){
        sizes.Add(c.Size);
        positions.Add(c.Location);
    }
    listsBuilt = true;
}

int count = 0;
foreach (Control c in this.Controls){
    c.Height = this.Height * (sizes[count].Height / SCALE_H);
    c.Width = this.Width * (sizes[count].Width / SCALE_W);
    c.Left = this.Width * (positions[count].X / SCALE_W);
    c.Top = this.Height * (positions[count].Y / SCALE_H);
}

It's worked great so far, and I've been able to make it global and apply it to all my forms. The problem comes when there are hidden objects on the form. I assumed that they would still be instantiated, and when the program first looped through to get the original size/positions of the objects it would pick them up, but that's not the case. If I switch modes on a form, and show some objects that weren't there before, then resize it, objects end up in the wrong places with the wrong sizes.

Is there some way for me to force it to instantiate hidden objects at form_load, even if they haven't been shown yet, so the lists will have the proper values for the proper controls at all times? If I absolutely have to, I could just do a quick loop at the start:

foreach (Control c in this.Controls){
    c.Show();
    c.Hide();
}

but it doesn't feel like a good way to go about it. It might make the application have an apparent flicker or perceived slowdown to the user. Any suggestions would be welcome (including suggestions relating to the method by which I'm scaling the form, if it's a particularly bad or inelegant way. I kind of stumbled to my current method with a little guesswork).

Edit: The hidden controls have their visibility set to "False" via the designer. They are shown via the .Show() function at a later point in the program.

Edit Again!: So it turns out that the visibility isn't even the issue. The issue is rather that as soon as a control is shown, it seems to shuffle around their index in the controls collection, so when I go back and compare to them they're not in the order that they were when I first filled the comparison List. That means that one control is likely to get compared to the values of another, and end up with that position and size rather than its own.

+1  A: 

Why not use a TableLayoutPanel instead? I think they would do everything you need, with 0 lines of code.

Daniel Rasmussen
Would a TableLayoutPanel also resize controls as well as reposition them?
KChaloux
Yes - Dock the `TableLayoutPanel` in your main form, then make as many rows/columns as you need. Controls can span multiple rows/columns with respective `Span` properties. Then anchor the controls to the sides of their cell you want them to resize to. For example, to have 4 buttons, each taking up a quarter of the screen, make a 2x2 `TableLayoutPanel`, place one button in each, and anchor each button to all four sides.
Daniel Rasmussen
Also, be sure to set all your rows and columns to percentages, not absolute sizes. (In the "Edit rows/columns dialogue.) See http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx for more details and examples. It's a great layout tool.
Daniel Rasmussen
A: 

If I understand you correctly you'd be much better off anchoring your controls. They'll scale up and down to maintain distance from the edges of their containing control and it'll all be handled for you by WinForms itself.

Martin Harris
I don't think anchoring is what the OP wants; anchoring maintains *absolute* distance between the edge of a control and its parent, whereas they're looking for *relative* distance.
Daniel Rasmussen
@Daniel Rasmussen: Precisely. As I have it now, the controls scale up and down in size along with the form, and maintain the same relative distance and ratio to the form itself and each other. I actually tried using Anchor/Dock pretty early on to do some scaling, but it just doesn't play nice with some of the non-standard National Instrument controls I'm using.
KChaloux
@KChaloux Just tried to reproduce your problem myself and I get data at form load for all the controls, whether they are visible, hidden or invisible. Can you update your question to show how you are hiding the controls? They are in the controls collection still, right?
Martin Harris
I think that's actually the bulk of the problem. They aren't in the controls collection until somewhere in the code they get explicitly ".Show()"n. somewhere in the code. They start out with the Visible property "false" in the designer.
KChaloux
@KChaloux In the test app that I just wrote (by dragging controls from the toolbox onto the form and using the property inspector to set visibility) the designer generated code put the controls in the collection and your loop correctly pulled out all the right sizes and locations. If your controls aren't in the collection of their parent then I assume you are controlling the addition somehow?
Martin Harris
That's bizarre... I'll have to check the designer and see if they're not being properly added to the collection. I bet that's my problem.
KChaloux