Fata1Attack

Hi,

My problem is a bit complicated ( for me for sure):

Imagine you have a form called Form1 and a usercontrol called UserControl1 placed on that forum and in Usercontrol1 you place a picturebox called Picturebox1. When you click the picturebox you want to know the exact top location.

the following code is wrong because it will give me the top-value of the picture inside the usercontrol not compared to the form:

Code Snippet

private void pictureBox1_Click(object sender, EventArgs e)

{

int y = pictureBox1.Top;

}

So I tried this, but it seems to go wrong at some point...:

Code Snippet

private void pictureBox1_Click(object sender, EventArgs e)

{

int y = Form1.MousePosition.Y - this.ParentForm.Top;

}



Re: Visual C# Express Edition Location of mousepointer (complicated)

IsshouFuuraibou

is your goal a relational position to your main form correct screen coordinates


int y = pictureBox1.Parent.Location.Y + pictureBox1.Location.Y;

or

Point ya = pictureBox1.PointToScreen(new Point(0, 0)); // Top left corner...
Point yt = this.PointToClient(ya);


remember your user control will have a location based on where it is in the form, and your picture box will have a location of where it is inside the user control.





Re: Visual C# Express Edition Location of mousepointer (complicated)

Fata1Attack

My goal is to know where the picturebox is exactly placed inside the forum.

and could you explain what this exactly means: pictureBox1.Parent.Location.Y

thank you!




Re: Visual C# Express Edition Location of mousepointer (complicated)

jrboddie

I think this will work.. PictureBox1.Location.Y gives the picturebox Y location relative to the control and this.Location.Y gives the location of the control relative to the form.

int y = pictureBox1.Location.Y + this.Location.Y;





Re: Visual C# Express Edition Location of mousepointer (complicated)

IsshouFuuraibou

pictureBox when added to a container gets a parent, the container in this case the user control area. The internal workings of the framework make sure that the picture box is in the controls of the container and the container becomes the pictureBox's parent. It's one method of going back from a control to where it is contained without needing to know the container.