Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click
Detecting the Mouse-Click Detecting the Mouse-Click
Detecting the Mouse-Click
Go Back  Xtreme Visual Basic Talk > > > Detecting the Mouse-Click


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2004, 12:18 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default Detecting the Mouse-Click


I am trying to place the coordinates of the cursor (within a form) into two variables upon clicking the mouse button. I found an excellent tip as to how to display the coordinates of the cursor as the mouse causes it to move:

http://vbcity.com/forums/faq.asp?fid...phics#TID59014

When moving the cursor, however, I would need the values of a particular set of coordinates placed in two variables through clickimg of the mose button. Thus, the mouse-click has to be detected somehow. I found the following tip to detect mouse-click:

http://www.codeguru.com/vb/gen/vb_mi...cle.php/c2757/

but I am having problems translating it into VB.NET. Does anyone know how this is done in V.NET?
Reply With Quote
  #2  
Old 06-28-2004, 12:55 PM
Iceplug's Avatar
IceplugDetecting the Mouse-Click Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Are you using the Form's MouseDown event to get the coordinates of the mouse, which would be located in the eventargs (e.X and e.Y)?

Also, the System.Windows.Forms.Cursor has some members that should tell you where the cursor is.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 06-28-2004, 01:06 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

this will put the x location and the y location into a label when you click on the form
Code:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim cp As Point = Me.PointToClient(Cursor.Position()) Label3.Text = (cp.X.ToString() & ", " & cp.Y.ToString()) End Sub
Reply With Quote
  #4  
Old 06-28-2004, 03:05 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by bear24rw
this will put the x location and the y location into a label when you click on the form
Code:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim cp As Point = Me.PointToClient(Cursor.Position()) Label3.Text = (cp.X.ToString() & ", " & cp.Y.ToString()) End Sub
It appears that the problem of mouse-click detection still remains. I’ll explain. Imagine you have a plot of a curve. You have it plotted once. With your code in, you will obtain only one pair of cursor coordinates and that’s going to be it. No matter how you move the mouse, the pair will be seen in the Label1 field unchanged. To have the coordinates of the cursor change as you move the cursor by the mouse you may place a timer with, say, 100 ms interval. Using Timer in this instance is not the best solution because it blinks somewhat which is annoying, but we will use the Timer here just as an example. Now, having the Timer refreshed every 100 ms, you can see the changing coordinates of the cursor displayed in Label1. So far so good. However, you now want only one particular set of these coordinates (corresponding to a particular position of the cursor) to be placed in two variables (one variable for the X coordinat and the other for the Y coordinate). One way to do it is through clicking on the mouse-button which should tell the program that this is the particular set of coordinates (and nothing else) that must be placed in two variables. Thus, you somehow need to detect the mouse-click so that only upon detection of that click the placement of the two coordinate values would occur.
Reply With Quote
  #5  
Old 06-28-2004, 03:32 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

you lost me....
Quote:
To have the coordinates of the cursor change as you move the cursor by the mouse
do you want to coordinates to change as you move the mouse??
Code:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove Dim cp As Point = Me.PointToClient(Cursor.Position()) Label3.Text = (cp.X.ToString() & ", " & cp.Y.ToString()) End Sub

EDIT:
ok... i think i get it
when you move the mouse display the coordinates in label1
but
if you click then you store those two coordinates and if you click again and again the original two coordinates will remain the same???
is that even close

Last edited by bear24rw; 06-28-2004 at 03:39 PM.
Reply With Quote
  #6  
Old 06-28-2004, 04:09 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by bear24rw
... if you click then you store those two coordinates and if you click again and again the original two coordinates will remain the same???
is that even close
Exactly … The variable(s) containing the original two coordinates should remain the same after the subsequent clicking. Now, upon clicking second time another set of constant coordinates should be placed in another set of variables. The third click should create a third set of variables containing a given set of coordinates and so on … It will be two or three clicks, however. No more. Because this is as much as I will need later in the program.
Reply With Quote
  #7  
Old 06-28-2004, 05:09 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

Code:
Dim pt(3) As Point Dim X As Integer Dim allowed As Boolean = True Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click If allowed = True Then X = X + 1 pt(X) = Me.PointToClient(Cursor.Position()) Label1.Text = (pt(X).X.ToString() & ", " & pt(X).Y.ToString()) If X = 3 Then allowed = False End If End If End Sub

try that
ask me if you dont understand something

(sry it took so long to reply... my computer decided that it was going to sound the defult beep every 5 seconds, not let me copy stuff and not let me log off)
Reply With Quote
  #8  
Old 06-28-2004, 05:59 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Gives me an error message: X_ is not a member of ‘System.Drawing.Point’.


P.S. X_ is the X in your code.

EDIT: Sorry, forgot to add -- above message appears when I implement your code in my project. Otherwise, it works just fine as is.
Reply With Quote
  #9  
Old 06-28-2004, 06:34 PM
Iceplug's Avatar
IceplugDetecting the Mouse-Click Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

X is a member of System.Drawing.Point,
X_ is not a member of System.Drawing.
The X that he has coming from the point should be an X.
The Dim X As Integer X can be replaced with X.

Label1.Text = (pt(X).X.ToString() & ", " & pt(X).Y.ToString())
The bold X and Y must be X and Y for a point.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #10  
Old 06-28-2004, 06:45 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Also, execution of the rest of the program, if any, until End Sub is reached should occur only after the two (or the three) variables are already formed. In your code it appears that anything after it (before the End Sub) will execute even before forming the said variables.
Reply With Quote
  #11  
Old 06-28-2004, 06:53 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by Iceplug
X is a member of System.Drawing.Point,
X_ is not a member of System.Drawing.
The X that he has coming from the point should be an X.
The Dim X As Integer X can be replaced with X.

Label1.Text = (pt(X).X.ToString() & ", " & pt(X).Y.ToString())
The bold X and Y must be X and Y for a point.
See, dimming it as X messes up everything in my program since there is an x already dimmed there. Besides, it is not just displayng the coordinates in Label1 that is needed. Three separate variables containing the coordinates have to form from the pt(X) as a result of the three-times clicking. Also, these three variables have to be formed prior to the exectution of whatever follows from the program.

I had that problem to begin with, that's why I started looking for solutions in the net.
Reply With Quote
  #12  
Old 06-28-2004, 08:19 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

wait... so does it work or not?
Reply With Quote
  #13  
Old 06-28-2004, 10:37 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by bear24rw
wait... so does it work or not?
It doesn't for my purposes, unfortunately ... The problem, as I see it, is, as Iceplug put it, X is a member of System.Drawing.Point, while X_ is not a member of System.Drawing. In other words, I can't have at the same time three different values of pt(X), if I understand it correctly. Also, the rest of the program doesn't wait until I gather the three values (through mouse-clicking). At least, I can't see for now how this could be done.
Reply With Quote
  #14  
Old 06-29-2004, 12:54 AM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by bear24rw
wait... so does it work or not?
Just to give you an idea what I’m trying to do … The form I am opening has the following structure:

Code:
Imports System.Drawing.Drawing2D
Imports System.IO

Public Class Form3
Inherits System.Windows.Forms.Form

<various Dim statements …>

[Windows Form Designer generated code]

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

<some code plotting a curve from a data-file>

<here should be the code we’re discussing>

<here’s some more code using the values from the code we’re discussing>

End Sub
End Class
The problems, as I see them now, are several:

First, X is reserved for cursor coordinate and conflicts with the variable x which I am already using.

Second, the code placed after the code we’re discussing, executes before the mouse-click-variables are formed, which shouldn’t happen, because that code needs the values of the said variables.

Also, when the form opens it paints only once and its further execution is not dependent on whether one clicks the mouse-button or not. Of course, we can apply the Timer method, I mentioned before – the Timer causing the form to re-paint every, say, 100 ms. Now the coordinate values change as the cusor is moving but how is the clicking of the mouse detected? Also, the use of Timer causes blinking and is very annoying. Some other method has to be sought for mouse-click detection.
Reply With Quote
  #15  
Old 06-29-2004, 09:57 AM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

use booleans to control when code is able to be executed and if the X for the variable is getting everything messed up then use a differnt letter.
Reply With Quote
  #16  
Old 06-29-2004, 10:44 AM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by bear24rw
use booleans to control when code is able to be executed and if the X for the variable is getting everything messed up then use a differnt letter.
Here is an idea … want to see what you think …

I create a new form (call it Form4) which is overlaid over the original form and is transparent. In Form4 I place the following code:

Code:
Public Class Form4
    Inherits System.Windows.Forms.Form

[Windows Form designer generated code]

    Dim pt(3) As Point
    Dim allowed As Boolean = True
    
Private Sub Form1_Click(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles MyBase.Click
        X = X + 1
        If X = 1 Then
            pt(X) = Me.PointToClient(Cursor.Position())
            X1 = pt(X).X.ToString()
        ElseIf X = 2 Then
            pt(X) = Me.PointToClient(Cursor.Position())
            X2 = pt(X).X.ToString()
            Me.Hide()
        End If
        Label1.Text = (X1 & ", " & X2)
        Label2.Text = (CStr(X))
    End Sub
End Class
‘ Note I am creating only two constants X1 and X2 holding the values of the two
‘ x-coordinates … this is all I need …
The new transparent form is called from the original Form3 and until the constants X1 and X2 are created through mouse-clicking the further execution of the code in the original Form3 is prevented. Also, I declared X, X1 and X2 in Module1 as Public integers so that I can have their values globally, and changed my original variable x to x_. Unfortunately, so far I can’t get the values of X1 and X2 created in Form4 transferred into Form3.
Reply With Quote
  #17  
Old 06-29-2004, 11:31 AM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

yeah... it would be easier to have it all on one form (but idk what you are trying to achive) but i just think that you need to get you variables strait and everything will work fine....
Reply With Quote
  #18  
Old 06-29-2004, 11:38 AM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Yeah, to have it all on one form would be better but I can't figure out how to achieve it. Calling another form is a way to prevent the execution of what comes after it until X1 and X2 are formed. How would that be done in one form beats me.
Reply With Quote
  #19  
Old 06-29-2004, 01:28 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Here is a question … As far as I understand, when you call Form4 from Form3 you don’t get back to Form3 to continue the exection of the rest of the procedure in Form3 until the work in Form4 is finished (until, say, certain Boolean conditions are fulfilled). Is there any way to stop the execution of the rest of the procedure within a sub until certain Boolean conditions are met?
Reply With Quote
  #20  
Old 06-29-2004, 03:17 PM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

i dont understand....

EDIT: i think i get it.... put everything inside the sub into a boolean

Code:
Dim allowed As Boolean Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If allowed = True Then 'do you stuff here end If End sub

and set the boolean to false until you are ready to execute it then set boolean to true and click the button and everything will work....

Last edited by bear24rw; 06-29-2004 at 03:37 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click Detecting the Mouse-Click
Detecting the Mouse-Click
Detecting the Mouse-Click
 
Detecting the Mouse-Click
Detecting the Mouse-Click
 
-->