Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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:

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();
                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…

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

Share

About the Author

diSant
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralI like the effects... PinmemberMatt Philmon8-Jan-03 6:02 
GeneralRe: I like the effects... PinmemberdiSant8-Jan-03 10:44 
GeneralRe: I like the effects... PinmemberMatt Philmon8-Jan-03 10:50 
GeneralRe: I like the effects... PinmemberdiSant8-Jan-03 11:17 
GeneralCould be more flexible PinmemberMarc Clifton6-Jan-03 4:27 
GeneralRe: Could be more flexible PinmemberdiSant6-Jan-03 8:17 
GeneralRe: Could be more flexible PinmemberMarc Clifton6-Jan-03 13:09 
GeneralPerformance Issue PinmemberHeath Stewart6-Jan-03 3:00 
GeneralRe: Performance Issue PinmemberdiSant6-Jan-03 7:51 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 5 Jan 2003
Article Copyright 2003 by diSant
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid