views:

121

answers:

2

how do i check if a mouse was clicked in a specific coordinate plane on the form"?

+1  A: 

You use the MouseEventArgs.X and MouseEventArgs.Y to see if they are within the coordinate plane.

This answer was one click away from the link I posted in my answer to on your previous question.

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs_members.aspx

Added

Scenario: I have a Rectangle shaped area I want to handle clicks in.

The top left corner of the shape is at location 28,83 (Left, Top)

The size is 225, 52 (width, height)

So if the location X (Left is between 28 and 28 + 225 (253) AND the location Y is between 83 and 83 + 52 (135) is within the bounds.

Code example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MouseClick += new MouseEventHandler(Form1_MouseClick);
        }

        void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.X >= 28 && e.X <= 253 && e.Y >= 83 && e.Y <= 135)
            {
                MessageBox.Show("Clicked within the rectangle");
            }
            else
            {
                MessageBox.Show("Clicked outside the rectangle");
            }
        }
    }
}
David Stratton
please give a code example
I__
hey david im sorry can you do vb.net please?
I__
+1  A: 

VB code for marking a 100 pixel square area starting at coord 100,100. (Set your own values.)

   Private Sub frm_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

      Dim x As Integer = e.Location.X
      Dim y As Integer = e.Location.Y

      If x > 100 AndAlso x < 200 AndAlso y > 100 AndAlso y < 200 Then
         MessageBox.Show("Inside")
      Else
         MessageBox.Show("Outside")
      End If

   End Sub

Of course, this is only going to trap the mouse click if it hits the form surface. You will have to think through what you want to do if you click on some control on the form.

Bill
Or you could use Dim x As Integer = e.X (My intellisense window was small and I saw the Location property first!)
Bill