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);
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)
{
int widthRatio = ClientRectangle.Width * 100 / startW;
int heightRatio = ClientRectangle.Height * 100 / startH;
foreach (Control ctl in Controls)
{
if ((ctl is Button)^(ctl is TextBox))
{
DefLocation dl2 = (DefLocation)hash[ctl];
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;
}
}
}