Results 1 to 5 of 5

Thread: Position of a click on a picturebox

  1. #1

    Thread Starter
    Lively Member drdress's Avatar
    Join Date
    Jul 2009
    Location
    Copenhagen, Denmark
    Posts
    76

    Position of a click on a picturebox

    If I double click my picturebox in the design screen, I get a click event method in my code:

    Code:
            private void PictureBox1_Click(object sender, EventArgs e)
            {
            }
    I know very little of the event syntax and class design, so I don't know how to get information from the event in question. I would like to know the coordinates where my mouse clicked the picturebox. Something like this:

    Code:
            private void PictureBox1_Click(object sender, EventArgs e)
            {
                    int x = e.X;
                    int y = e.Y;
            }
    I know this won't work. But how should this be done?

  2. #2
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Position of a click on a picturebox

    Quote Originally Posted by drdress View Post
    If I double click my picturebox in the design screen, I get a click event method in my code:

    Code:
            private void PictureBox1_Click(object sender, EventArgs e)
            {
            }
    I know very little of the event syntax and class design, so I don't know how to get information from the event in question. I would like to know the coordinates where my mouse clicked the picturebox. Something like this:

    Code:
            private void PictureBox1_Click(object sender, EventArgs e)
            {
                    int x = e.X;
                    int y = e.Y;
            }
    I know this won't work. But how should this be done?
    Most events will have 'EventArgs' that contains information regarding the event. As you can see below the MouseClick event for a picture box has 'MouseEventArgs' which contains the coordinates of where you clicked.

    Code:
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
                int xCoordinate = e.X;
                int yCoordinate = e.Y;
            }

  3. #3
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Position of a click on a picturebox

    really MouseEventArgs object have x and y property which specify the coordinates of where your mouse is cliked .
    Code:
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
                int xCoordinate = e.X;
                int yCoordinate = e.Y;
            }

  4. #4

    Thread Starter
    Lively Member drdress's Avatar
    Join Date
    Jul 2009
    Location
    Copenhagen, Denmark
    Posts
    76

    Re: Position of a click on a picturebox

    Well, as I wrote when I double click the picturebox (called pbProfile) I get the method:

    Code:
            private void PictureBox1_Click(object sender, EventArgs e)
            {
            }
    This does not contain an event arg called MouseEventArgs, but only EventArgs which doesn't have an x- and y-coordinate. So how do I get the method you wrote:

    Code:
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
            }

  5. #5
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Position of a click on a picturebox

    when you write dot(.) just after e .you will get their Methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width