1

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

2 Answers2

2

Use global variables. Assign the values to them and access anywhere in your class.

Kingsman
  • 174
  • 1
  • 10
  • Global variables are almost always a bad choice. You need more information from the questioneer to give a good answer because you don't know why he can't use them. For example you won't use global variables when both methods are in the same class. – Mighty Badaboom Apr 20 '17 at 08:08
  • 1
    I know global variables are bad but they are necessary evil some times. You're right mate, more info would be good but I thought this would help him. – Kingsman Apr 20 '17 at 08:16
  • i also think in Winform GDI+ the only way to share mouse location is global values. – Lei Yang Apr 20 '17 at 08:34
  • Many thanks.... !!! I assume that @Ive solution is using global variables.. ???? This is my first go at OOP and not having global variables as a 'standard' way of passing arguments I was a bit lost... Still... I have just had a crash course on Events and Delegates as I thought that would be the answer. What is the Winform GDI+ that Lei mentioned? – Steve Galvin Apr 21 '17 at 13:50
2

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

Ive
  • 1,321
  • 2
  • 17
  • 25