Reputation: 10967
My problem is : I have 2 Panels (panel1,panel2) where panel1.Size = new Size(200, 200);
and Panel2.Size = new Size(600, 600);
where both panel's have within a CustomControl which can get Dragged and change it Possition (szbControl1 ,szbControl2) .
My Question is ,how can i set szbControl2.Location
properly (proportionally)based on szbControl1.Location
where szbControl1
parent is panel1
and szbControl2
parent is panel2
,like if i move the szbControl1
at bottom also szbControl2
should be at bottom.
So far i tried this :
private void sizeAbleCTR2_LocationChanged(object sender, EventArgs e)
{
int smallX = (sizeAbleCTR2.Location.X * panel1.Size.Width) / 100;
int smallY = (sizeAbleCTR2.Location.Y * panel1.Size.Height) / 100;
int largeX = (smallX * panel2.Width) / 100;
int largeY = (smallY * panel2.Height) / 100;
sizeAbleCTR1.Location = new Point(largeX,largeY);
}
like using the Percentage but it's not working .
Upvotes: 0
Views: 4920
Reputation: 7326
The code you provided does not take into account the size of the szbControls. The ratio of the (location/the differences of the sizes) should be equal.
private void sizeAbleCTR2_LocationChanged(object sender, EventArgs e)
{
float srcHeightDiff = panel2.Height - sizeAbleCTR2.Height;
float dstHeightDiff = panel1.Height - sizeAbleCTR1.Height;
int locY = (int)(dstHeightDiff * (sizeAbleCTR2.Location.Y / srcHeightDiff));
float srcWidthDiff = panel2.Width - sizeAbleCTR2.Width;
float dstWidthDiff = panel1.Width - sizeAbleCTR1.Width;
int locX = (float)(dstWidthDiff * (sizeAbleCTR2.Location.X / srcWidthDiff));
sizeAbleCTR1.Location = new Point(locX, locY);
}
Upvotes: 3