Get click point on a form ?

maitung

New member
Joined
Nov 13, 2005
Messages
4
Programming Experience
Beginner
Hello everyone, I'm a newbie in VB.NEt, plz help me with this.
I need to get the point of the mouseclick on the form. How to get that ? I know I must process the Click event, but I don't know what to do inside that Sub.
thanks for reading.
 
Ok i'm as bit of a newbie myself. but you could ty this.

It is possible to get the position of the cursor at anytime in your code by using the Shared Cursor.Position method. To return this as a value inside the client region you should use The PointToClient Method.

In an example :

[Me.location.contains(pointtoclient(me.mouseposition)) = true]

Where me is the form. Sorry if the code is not quite right doing it from memory. Hope this helps.
 
I have a custom control, it is just a chessboard made up of 64 buttons. I add the chessboard control to the form and display it. When users click on the chessboard, I wanna get back which button of the chessboard is clicked. Below is what I do:

VB.NET:
public class Screen

    dim WithEvents chessBoard as Control
    'loop to add button to board
    For ....
        ' create button here then add....
        chessBoard.Controls.Add( button)
    Next
    chessBoard.Location = ...    
    'add to the form
    Me.Controls.Add(chessBoard)
    
    Private Sub chessBoard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chessBoard.Click
        ' what should I do here ??

       End Sub
    
    'I also try to get mouse position relative to the chessboard control, but the event isn't raise
     Private Sub chessBoard_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chessBoard.MouseDown



        End Sub

End Class
So how to do it , plz? Anything wrong with what I did ? thanks.
 
It sounds to me that you don't need to know where the mouse button is. If you have added buttons to the board then you should just be able to reference which button is clicked, either by it's name or by using the 'Sender' as object.
 
Create a seperate method that has the signature of a button's click event handler (that method will handle the click event for all the buttons that you create in the loop). In the loop use addHandler to link each button to that method. In the method use the 'sender' parameter to obtain a reference to the button that was clicked.
VB.NET:
Private Sub ...
    For ...
        Dim btn As New Button()
        'set the properties of the button
        chessBoard.Controls.Add(btn)
        AddHandler btn.Click, AddressOf chessBoardButton_Click
    Next
End Sub
   
Private Sub chessBoardButton_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs)
    Dim btn As Button = CType(sender, Button)
    'btn is the button that was clicked
End Sub
 
Back
Top