Click here to Skip to main content
15,395,276 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I am using a picturebox in my form and it has its sizemode property set to 'StretchImage'.I am populating dynamically created buttons inside this picturebox ,the locations of which have already been set using another form and is updated in the database.My problem is that when the image in the picturebox gets stretched,the locations of the controls remain the same(as in the database).I want them to be relocated relative to the stretched image.Is this possible?Please help me out........

Sunitha
Posted
Comments
Tarun.K.S 28-Oct-10 5:46am
   
well one solution can be by "binding" the size of the button with that of the image.
qontary 28-Oct-10 5:49am
   
It will be easier for others to point you to the right direction if you share some of the code.
sunitkrishna 28-Oct-10 5:55am
   
Here is what i do to place the buttons dynamically.
using (DataSet ds = new LayoutDAL().GetLayoutDetails(_layoutId))
{
Button bt = new Button();
bt.SetBounds(Convert.ToInt32(ds.Tables[0].Rows[i]["LocationX"]), Convert.ToInt32(ds.Tables[0].Rows[i]["LocationY"]), 60,60);
}

I have set 60x60 as the size of the button.The sizemode property set to 'StretchImage'by design.

1 solution

Try this.

C#
int originalPictureWidth = 50; //you should know the original size
int originalPictureHeight = 50;
double xScale = (double)pictureBox1.Width / (double)originalPictureWidth;
double yScale = (double)pictureBox1.Height / (double)originalPictureHeight;
using (DataSet ds = new LayoutDAL().GetLayoutDetails(_layoutId))
{
    Button bt = new Button(); bt.SetBounds(
        (int)(Convert.ToDouble(ds.Tables[0].Rows[i]["LocationX"]) * xScale),
        (int)(Convert.ToDouble(ds.Tables[0].Rows[i]["LocationY"]) * yScale), 60, 60);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900