dotnetmonitor.com |
|
||||||||||||
| Problem: There are several properties in
which you can change the size/location of a control. These include, but are not
limited to: Width, Height, Top, Bottom, Left, Right, Size, Location, Bounds,
and ClientSize. Setting panel1.Width = 10 and panel1.Height = 10 causes twice
the work to occur than setting them both together (especially after the handle
has been created as windows forms is chatting live with the operating system
about what the size should really be.)
Solution: Do all your calculations, then set the property that reflects the most information you have. If you're just chaning the Size, set Size, if you're changing the Size and Location change Bounds. private void button1_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch();
sw.Start(); for (int i = 1; i < 1000; i++) { panel1.Width = i; panel1.Height = i; } sw.Stop();
Stopwatch sw2 = new Stopwatch(); sw2.Start(); for (int i = 1; i < 1000; i++) { panel1.Size = new Size(i, i); } sw2.Stop(); MessageBox.Show(String.Format("Trial 1 {0}\r\nTrial 2 {1}", sw.ElapsedMilliseconds, sw2.ElapsedMilliseconds)); }
// As expected, the second one takes about 1/2 the time because there's half the conversation going on. // Trial 1 3717 // Trial 2 1865 |
||||||||||||
|
||||||||||||