Click here to Skip to main content
Licence 
First Posted 4 Jan 2003
Views 50,014
Bookmarked 16 times

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

By | 4 Jan 2003 | Article
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

About the Author

diSant

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI like the effects... PinmemberMatt Philmon6:02 8 Jan '03  
GeneralRe: I like the effects... PinmemberdiSant10:44 8 Jan '03  
GeneralRe: I like the effects... PinmemberMatt Philmon10:50 8 Jan '03  
GeneralRe: I like the effects... PinmemberdiSant11:17 8 Jan '03  
GeneralCould be more flexible PinmemberMarc Clifton4:27 6 Jan '03  
GeneralRe: Could be more flexible PinmemberdiSant8:17 6 Jan '03  
GeneralRe: Could be more flexible PinmemberMarc Clifton13:09 6 Jan '03  
GeneralPerformance Issue PinmemberHeath Stewart3:00 6 Jan '03  
GeneralRe: Performance Issue PinmemberdiSant7:51 6 Jan '03  

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.

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