Hack Forums
[C#] [C#] Auto controls resizer - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#] Auto controls resizer (/showthread.php?tid=5940159)



[C#] Auto controls resizer - Anon Scripter - Feb 11, 2019

Hi. When i first started coding, i had trouble with make an auto-resizer for the forums. But now, since i have 6 years of experience, it was hella easy to make, so im releasing it! :)
 
(Dont forget to vouch  🤝  )
The good stuff:
This goes under SizeChanged:
Code
           int nextH = (this.Height - current.Height);
           int nextW = (this.Width - current.Width);
           foreach(Control c in controls.Keys)
           {
               if (controls[c]) // If you want the height to be resized also (Very nifty)
               {
                   c.Height += nextH;
               }
               c.Width += nextW;
           }
           current = this.Size;
           this.Text = string.Format("YourForm ({0}, {1})", this.Width, this.Height); //display the width and height (Not needed, just nifty for working on minimumsize)
Declare this in global form:
Code
       Size current = new Size();
       Dictionary<Control, bool> controls = new Dictionary<Control, bool>();
How to use:
Put this in Form_Load:
Code
           current = this.Size;
           controls.Add(this.YourControlWithHeightResize, true);
           controls.Add(this.YourControlNoHeightResize, false);
Enjoy!   👍   - Anon Scripter


RE: [C#] Auto controls resizer - 0xDEAD10CC - Feb 11, 2019

What's wrong with using the Anchor property? 6 years and you haven't discovered that yet? 🤨

Code
Size current = new Size();
"current" is actually probably not the best name either since it indicates the Size of the control before the size changed, which definitely implies past-tense.

I also don't see why you're using a Dictionary like this. It doesn't do anything more than a simple collection would because you're still looping through everything anyways by iterating the keys, and doing a lookup from there, rather than just looping through all of the KeyValuePair<TKey, TValue> entities. As long as the hashcode is unique the only thing it does is prevent the same control from being added.