Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi if we have a panel with a picture Box in it(panel.size>picture Box.size)
can we find any relation between autoScrollPosition of the panel and x,y of a point in the picture Box?

best regards.
Posted
Updated 21-Jul-11 7:01am
v3

1 solution

The relationship between the two is that the AutoScrollPosition is offsetting the PictureBox.Location relative to the Panel.Location, and the point in the PictureBox is an offset from the PistureBox.Location.

[Edit]
To change the AutoScrollPosition, you need to assign it a Point object that represents the offset that you want it to move to. In other words:
panel1.AutoScrollPosition = new Point(50,100);

would cause the horizontal scrollbar to move 50 points to the right and the vertical scrollbar would move down 100 points. It sounds like you are trying to display a map and have it automatically center when the user clicks on it, is that right? If so, your Mouseclick event would do something like this:

C#
private void pictureBox1_MouseClick ( object sender, MouseEventArgs e )
{
	Point panelcenter = new Point ( ( this.panel1.Width / 2 ), ( this.panel1.Height / 2 ) ); // find the centerpoint of the panel
	Point offsetinpicturebox = new Point ( ( this.pictureBox1.Location.X + e.Location.X ), ( this.pictureBox1.Location.Y + e.Location.Y ) ); // find the offset of the mouse click
	Point offsetfromcenter = new Point ( ( panelcenter.X - offsetinpicturebox.X ), ( panelcenter.Y - offsetinpicturebox.Y ) ); // find the difference between the mouse click and the center

	this.panel1.AutoScrollPosition = new Point ( 
		( Math.Abs ( this.panel1.AutoScrollPosition.X ) + ( -1 * offsetfromcenter.X ) ), 
		( Math.Abs ( this.panel1.AutoScrollPosition.Y ) + ( -1 * offsetfromcenter.Y ) ) 
	);
}


Note: It is important to realize that when you retrieve the AutoScrollPosition, it will return with negative values. However, when you assign it, you need to assign positive values.
 
Share this answer
 
v3
Comments
shiny1366 21-Jul-11 15:48pm    
thank JOAT-MON
how can I transfer a point from picture box coordinate to panel coordinate?I meant this. or if we had a point in the picrure what its x or y will be in the panel?
JOAT-MON 21-Jul-11 16:26pm    
I think the point's offset relative to the Panel.Location = PictureBox.Location + PointInPictureBox + AutoScrollPosition
shiny1366 24-Jul-11 15:37pm    
thanks JOAT-MON.
but it didnt work!
JOAT-MON 25-Jul-11 13:38pm    
I need you to give me a better description of what "it doesn't work" means for me to help. Are you getting an error? Is the resulting point not where you think it should be? Are you certain the point you are trying to calculate is being captured by the PictureBox (as opposed to being captured by the Panel)? Can you share the part of your code where you are trying to calculate this?
JOAT-MON 25-Jul-11 14:53pm    
Ok, just created a sample project, during run-time if you scroll the panel, the PictureBox.Location will already reflect the offset of the AutoScrollPosition. If you are getting a point using one of the Mouse events on the PictureBox, then the offset from the Panel.Location of the point will be:

OffsetFromPanelLocation = PointInPictureBox + PictureBox.Location

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