Click here to Skip to main content
13,042,421 members (126,677 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 4 Jan 2003

Calculator with Proportionaly Resizable Controls. Using Hashtable() to store default locations of controls.

, 4 Jan 2003
Rate this:
Please Sign up or sign in to vote.
This article shows you how to use Hashtable() to store data, foreach() to cycle through collections of elements, and some other functions that you can usually see in most of the applications.

Sample Image - kalkulator.jpg


The best way to resize controls that are of different size and type would be – to store the default (start) value of every Control that needs to be resized. It is particularly useful when working with Controls of different sizes and locations. This can be achieved either by performing a deep copy of the original object or copying the parameters of the original object into a custom object. Because Button doesn’t support Clone(), a custom object needs to be created:

public class DefLocation

    int defW,defH,defCX,defCY; 
    public void setDefW(int dw){
       defW = dw;
    public void setDefH(int dh){
       defH = dh;
    public void setDefCX(int dcx){
       defCX = dcx;
    public void setDefCY(int dcy){
       defCY = dcy;

    public int getDefW(){return defW;}
    public int getDefH(){return defH;}
    public int getDefCX(){return defCX;}
    public int getDefCY(){return defCY;}

… cycle through the Controls copying their values and store them in a HashTable hash for future retrieval.

Hashtable hash = new Hashtable();
    public void InitDefLoc()
        foreach(Control cl in Controls)
            if ((cl is Button) ^ (cl is TextBox))
                DefLocation dl = new DefLocation();

Now every Button and every TextBox have their default parameters saved inside the HashTable hash. Now – the most exciting part – the resizing routine…

private void Form1_Resize(object sender, System.EventArgs e)
    // calculate the resize ratio
    int widthRatio = ClientRectangle.Width * 100 / startW;
    //startW/H – hardcoded size of the application
    int heightRatio = ClientRectangle.Height * 100 / startH;
    foreach (Control ctl in Controls)
        if ((ctl is Button)^(ctl is TextBox))
            // for every control, access its default values stored in hash
            DefLocation dl2 = (DefLocation)hash[ctl];

            // need a new Point
            Point pnt = new Point((dl2.getDefCX() * widthRatio / 100),
                               (dl2.getDefCY() * heightRatio / 100));

            ctl.Width = dl2.getDefW() * widthRatio / 100;
            ctl.Height = dl2.getDefH() * heightRatio / 100;
            ctl.Location = pnt;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralI like the effects... Pin
Matt Philmon8-Jan-03 6:02
memberMatt Philmon8-Jan-03 6:02 
GeneralRe: I like the effects... Pin
diSant8-Jan-03 10:44
memberdiSant8-Jan-03 10:44 
GeneralRe: I like the effects... Pin
Matt Philmon8-Jan-03 10:50
memberMatt Philmon8-Jan-03 10:50 
GeneralRe: I like the effects... Pin
diSant8-Jan-03 11:17
memberdiSant8-Jan-03 11:17 
GeneralCould be more flexible Pin
Marc Clifton6-Jan-03 4:27
memberMarc Clifton6-Jan-03 4:27 
GeneralRe: Could be more flexible Pin
diSant6-Jan-03 8:17
memberdiSant6-Jan-03 8:17 
GeneralRe: Could be more flexible Pin
Marc Clifton6-Jan-03 13:09
memberMarc Clifton6-Jan-03 13:09 
GeneralPerformance Issue Pin
Heath Stewart6-Jan-03 3:00
memberHeath Stewart6-Jan-03 3:00 
GeneralRe: Performance Issue Pin
diSant6-Jan-03 7:51
memberdiSant6-Jan-03 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 5 Jan 2003
Article Copyright 2003 by diSant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid