Search code examples
c#mouseeventpictureboxmouse-position

How can I use the MouseEventArgs in another method?


I have a picture box which I need to get the values of the mouse position within the box once the mouse is clicked in it. I can do this using this code:

public void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {

        int CurX;
        int CurY;
        CurX = e.X;
        CurY = e.Y;
    }

I now need to use those values CurX and CurY to convert them onto relative positions of the picture box ie the four corners of the picture box have GPS Lat and Long coordinates so the approximate Lat and Long can be calculated from the position of the mouse event click in the picture box.

The calculated values are checked against an array in another method and values selected from the array based on the approximate Lat and Long values.

How can I get these CurX and CurY values from pictureBox1_MouseClick to another method and then use another MouseClick event and repeat the process?

Many thanks Steve


Solution

  • Use:

    private int curX;
    private int curY;
    
    public void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        curX = e.X;
        curY = e.Y;
    }
    

    Then you can use curX a curY at other places in the class