0

Im working on a battleships program and have been trying to find a way to detect if my mouse click is on the form or on the picturebox using c# wpf.

if (click on form)
{
    do this 
}
else
{
    do this
} 
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • possible duplicate of [How to get control under mouse cursor?](http://stackoverflow.com/questions/2411062/how-to-get-control-under-mouse-cursor) – GETah May 28 '12 at 12:16
  • 1
    usually mouseclick events are registered against a form or a control ? how have you registered the event in your application and against what control ? – Habib May 28 '12 at 12:17
  • 1
    @GETah - thats for windows forms. – Daniel A. White May 28 '12 at 12:18
  • 1
    Don't all the controls in WPF have various mouse-click events? Can you register a mouse event on the picturebox control separately from the background form? – Chris Sinclair May 28 '12 at 12:56

1 Answers1

1

If you go to the properties of the picturebox you are using and select the events tab there should be an "mouseclick" event.

Axxelsian
  • 787
  • 3
  • 9
  • 25
  • Thanks but not quite what I mean I have pictureboxes copying using arrays what i want is different events for if I click on the form and else if it clicks on a any picturebox if – Peter Luck May 28 '12 at 12:41
  • You can use the event argument's mouse coordinates to determine where it was clicked to determine if it is on your form, or anything else. – Axxelsian May 28 '12 at 13:01
  • 1
    protected void Form1_MouseClick(object sender, MouseEventArgs click) { mouseClickX.Text = click.X.ToString(); mouseClickY.Text = click.Y.ToString(); – Peter Luck May 28 '12 at 13:10
  • this is what i have to tell my location then im trying to get it so if it clicks on a picturebox it prints 1 image(out of an array) and if i click on the form it prints a different one (using array again) with the mouse co ords stopping and just reading as the top corner of the picturebox how do i go about telling what is form and what is picturebox if you could show little bit of code about how to do it would be great im new to programming and not quite sure on the arguments i should use. – Peter Luck May 28 '12 at 13:16
  • If you select the form, or any item on it, under properties you can see their location, and also their size. Using that, you can determine where your picturebox or list is (i have no idea what your layout is like, so i cant really show much code) and use the location and size to determine your if statement that says if(inside this area where picturebox is) do this, if else(inside the arraylist) do this, else display invalid text. – Axxelsian May 28 '12 at 13:53
  • 1
    Thanks sorted it out had had to get the location and size to come out of the origional arrays before i could do it – Peter Luck May 28 '12 at 13:56
  • You should choose an answer when asking a question so that when you ask one in the future, people will answer them. If not, they wont think that they will give you the credit. – Axxelsian May 29 '12 at 13:20