Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > Help with mouse coordinates!


Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2008, 04:37 PM
Knoxvillage Knoxvillage is offline
Newcomer
 
Join Date: May 2008
Posts: 2
Default Help with mouse coordinates!

Hello,

I was wondering if there is anyway I can get mouse coordinates when I click on a mouse button inside a listbox? I know how to do it in the form but when I click inside a listbox nothing happens.

Can anyone help me?
Reply With Quote
  #2  
Old 06-02-2008, 05:41 AM
Machaira's Avatar
Machaira Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

Why do you need this?
Reply With Quote
  #3  
Old 06-12-2008, 02:50 PM
optox's Avatar
optox optox is offline
Freshman
 
Join Date: Mar 2008
Posts: 49
Default

can you post the code that you have already tried?
Reply With Quote
  #4  
Old 06-20-2008, 10:26 AM
andrewisback andrewisback is offline
Newcomer
 
Join Date: Jun 2008
Posts: 4
Default

Did any one reply to this post because i want to do a similar thing. i want to get the mouse co-ordinates with in a picture when clicked on i tried google but the code i could find seem to be out of date. This statement is no longer used use 'structure' so...but i'll keep looking

Thanks in advance
Reply With Quote
  #5  
Old 06-20-2008, 12:49 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

First, you have to understand that there's multiple coordinate systems at play here. The first coordinate system is called screen coordinates. In this coordinate system, (0, 0) is located at the top left corner of your primary monitor. Positive x is to the right, positive y is down. The second coordinate system is called client coordinates. In this coordinate system, (0, 0) is located at the top left corner of a control, with the same axis direction as screen coordinates (barring any transforms). Any control can translate a point from one system to the other by using its PointToScreen or PointToClient methods.

If you handle a control's MouseUp or MouseDown event, you will receive a MouseEventArgs as a parameter. This has a Location property, along with X and Y to tell you where the mouse was when the event was generated. Unfortunately, it is not documented whether these coordinates are in client coordinates or screen coordinates; you should be able to determine quickly with a test application.

The Click event only provides an EventArgs parameter, which doesn't have any cursor location information. In this case, you use the System.Windows.Forms.Cursor class to find the location. It's Position property gives you the mouse position in screen coordinates.

Here's a form that demonstrates the more cumbersome Cursor.Position method:
Code:
Public Class DemoForm
    Inherits Form

    Dim _picBox As PictureBox

    Public Sub New()
        _picBox = New PictureBox()
        _picBox.BackColor = Color.DarkGreen
        _picBox.Location = New Point(0, 0) ' client coordinates relative to the form
        _picBox.Dock = DockStyle.Fill
        AddHandler _picBox.Click, AddressOf PicBoxOnClick

        Me.Padding = New Padding(25)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Controls.Add(_picBox)
    End Sub

    Private Sub PicBoxOnClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim screenLocation As Point = Cursor.Position
        Dim clientLocation As Point = _picBox.PointToClient(screenLocation)
        MessageBox.Show(clientLocation.ToString())
    End Sub

End Class
Personally, I prefer using MouseUp to take advantage of the convenient MouseEventArgs, which in this case seems to use client coordinates:
Code:
Public Class DemoForm
    Inherits Form

    Dim _picBox As PictureBox

    Public Sub New()
        _picBox = New PictureBox()
        _picBox.BackColor = Color.DarkGreen
        _picBox.Location = New Point(0, 0) ' client coordinates relative to the form
        _picBox.Dock = DockStyle.Fill
        AddHandler _picBox.MouseUp, AddressOf PicBoxOnMouseUp

        Me.Padding = New Padding(25)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Controls.Add(_picBox)
    End Sub

    Private Sub PicBoxOnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        MessageBox.Show(e.Location.ToString())
    End Sub

End Class
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 06-20-2008, 01:28 PM
andrewisback andrewisback is offline
Newcomer
 
Join Date: Jun 2008
Posts: 4
Default

I don't understand, whats with the picture box. From what i can make out that makes a new picture box or something

The event on which i wish to take the co-ordinates is when the picture box is clicked on which is simple enough and i can do.

But what i want is to retrieve the mouse co-ordinates by a function and out put them as a string value in reference to the form as the form can be moved so system coordinates would be useless.
See the picture is a map and when you click on it you will be inputing you wish a desired thing to happen at this location. The program then out puts it into a line of code that is made for a server client (irrelevant but back ground) all this i can do i am just stuck on the co-ordinates
Reply With Quote
  #7  
Old 06-20-2008, 01:46 PM
andrewisback andrewisback is offline
Newcomer
 
Join Date: Jun 2008
Posts: 4
Default

I sort of worked something out its very simple and basic

xis = Control.MousePosition.X - Me.Location.X
yis = Control.MousePosition.Y - Me.Location.Y
Label1.Text = xis.ToString + "," + yis.ToString

It gives me the co-ordinates, probably not perfect but i'll use it for now
Reply With Quote
  #8  
Old 06-20-2008, 02:13 PM
Roger_Wgnr's Avatar
Roger_Wgnr Roger_Wgnr is offline
CodeASaurus Hex

Forum Leader
* Expert *
 
Join Date: Jul 2006
Location: San Antonio TX
Posts: 2,335
Default

I would make one change.
Label1.Text = xis.ToString + "," + yis.ToString
Change to
Label1.Text = xis.ToString & "," & yis.ToString
Use of the + operator to join strings is not a good idea.
It can result in unexpected results always use the & to join strings.
__________________
Code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. ~Martin Golding
The user is a peripheral that types when you issue a read request. ~Peter Williams
MSDN Visual Basic .NET General FAQ
Reply With Quote
  #9  
Old 06-23-2008, 08:11 AM
andrewisback andrewisback is offline
Newcomer
 
Join Date: Jun 2008
Posts: 4
Default

Cheers mate i will remember that in future
Reply With Quote
  #10  
Old 06-23-2008, 08:41 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

My example was completely relevant to your problem. Did you run it or did you just glance at it then reject it, thus wasting the time I spent explaining things?

It creates a picture box and foregoes using an image in favor of a background color. When you click on this picture box, it displays a message box with the coordinates of the mouse click within the picture box. How is this not relevant?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
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:

Powered by liquidweb