12,453,354 members (60,282 online)
alternative version

54.7K views
16 bookmarked
Posted

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

, 4 Jan 2003
 Rate this:
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.

## 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 `Control`s 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);
}
}
}```

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;
}
}
}```

A list of licenses authors might use can be found here

## Share

 Web Developer United States
No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 I like the effects... Matt Philmon8-Jan-03 6:02 Matt Philmon 8-Jan-03 6:02
 Re: I like the effects... diSant8-Jan-03 10:44 diSant 8-Jan-03 10:44
 Re: I like the effects... Matt Philmon8-Jan-03 10:50 Matt Philmon 8-Jan-03 10:50
 Re: I like the effects... diSant8-Jan-03 11:17 diSant 8-Jan-03 11:17
 Could be more flexible Marc Clifton6-Jan-03 4:27 Marc Clifton 6-Jan-03 4:27
 Re: Could be more flexible diSant6-Jan-03 8:17 diSant 6-Jan-03 8:17
 Re: Could be more flexible Marc Clifton6-Jan-03 13:09 Marc Clifton 6-Jan-03 13:09
 Performance Issue Heath Stewart6-Jan-03 3:00 Heath Stewart 6-Jan-03 3:00
 Re: Performance Issue diSant6-Jan-03 7:51 diSant 6-Jan-03 7:51
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Aug-16 1:02 Refresh 1