Click here to Skip to main content
15,880,967 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
4 Jan 2003 60.6K   663   16   9
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

Introduction

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:

C#
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.

C#
Hashtable hash = new Hashtable();
………
    public void InitDefLoc()
    {
        foreach(Control cl in Controls)
        {
            if ((cl is Button) ^ (cl is TextBox))
            {
                DefLocation dl = new DefLocation();
                dl.setDefW(cl.Width);
                dl.setDefH(cl.Height);
                dl.setDefCX(cl.Location.X);
                dl.setDefCY(cl.Location.Y);
                hash.Add(cl,dl);
            }
        }
    }

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

C#
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;
        }
    }
}

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI like the effects... Pin
Matt Philmon8-Jan-03 6:02
Matt Philmon8-Jan-03 6:02 
GeneralRe: I like the effects... Pin
diSant8-Jan-03 10:44
diSant8-Jan-03 10:44 
GeneralRe: I like the effects... Pin
Matt Philmon8-Jan-03 10:50
Matt Philmon8-Jan-03 10:50 
GeneralRe: I like the effects... Pin
diSant8-Jan-03 11:17
diSant8-Jan-03 11:17 
GeneralCould be more flexible Pin
Marc Clifton6-Jan-03 4:27
mvaMarc Clifton6-Jan-03 4:27 
GeneralRe: Could be more flexible Pin
diSant6-Jan-03 8:17
diSant6-Jan-03 8:17 
GeneralRe: Could be more flexible Pin
Marc Clifton6-Jan-03 13:09
mvaMarc Clifton6-Jan-03 13:09 
GeneralPerformance Issue Pin
Heath Stewart6-Jan-03 3:00
protectorHeath Stewart6-Jan-03 3:00 
GeneralRe: Performance Issue Pin
diSant6-Jan-03 7:51
diSant6-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.