foreach Control loop misses hidden controls
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开发者_Python百科 another, and end up with that position and size rather than its own.
Why not use a TableLayoutPanel
instead? I think they would do everything you need, with 0 lines of code.
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.
精彩评论