Results 1 to 6 of 6

Thread: Click inside a list of points to return a number

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Click inside a list of points to return a number

    I created a list of points as the first section of my image map. There will eventually be 80+ sections each with their own number value.

    If anybody can tell me how to make this particular section return a value of 20 to a label when the mouse is clicked inside the coordinates I laid out, that would be great!

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.  
    5.         Dim pts As New List(Of PointF)() 's20 big
    6.  
    7.         pts.Add(New PointF(224, 75))
    8.         pts.Add(New PointF(232, 131))
    9.         pts.Add(New PointF(267, 131))
    10.         pts.Add(New PointF(276, 75))
    11.         pts.Add(New PointF(224, 75))
    12.  
    13.         Me.InitializeComponent()
    14.  
    15.         Me.PictureBox1.Image = My.Resources.Dartboard_unlabeled_svg
    16.  
    17.     End Sub
    18.  
    19.     Private Sub PictureBox1_mouseclick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
    20.  
    21.     End Sub
    22.  
    23. End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Click inside a list of points to return a number

    you could use graphicspaths with a dictionary(of graphicspath, integer) like this:

    Code:
    Public Class Form1
    
        Dim sections As New Dictionary(Of Drawing2D.GraphicsPath, Integer)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim pts As New List(Of PointF)() 's20 big
            pts.Add(New PointF(224, 75))
            pts.Add(New PointF(232, 131))
            pts.Add(New PointF(267, 131))
            pts.Add(New PointF(276, 75))
            pts.Add(New PointF(224, 75))
    
            Dim gp As New Drawing2D.GraphicsPath
            gp.AddPolygon(pts.ToArray)
    
            sections.Add(gp, 20)
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
            Dim kvp As KeyValuePair(Of Drawing2D.GraphicsPath, Integer) = sections.FirstOrDefault(Function(de) de.Key.IsVisible(e.Location))
            MsgBox(kvp.Value)
        End Sub
    
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            Dim kvp As KeyValuePair(Of Drawing2D.GraphicsPath, Integer) = sections.FirstOrDefault(Function(de) de.Key.IsVisible(e.Location))
            Me.Text = (kvp.Value = 20).ToString
        End Sub
    
    End Class
    edit: the MouseMove code was just there for my testing purposes...
    Last edited by .paul.; May 5th, 2013 at 05:23 PM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Click inside a list of points to return a number

    any progress with that?

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Click inside a list of points to return a number

    That looks exactly like what I need!

    Only thing is when using that code the image in the picture box doesn't show and the picture box blocks mouse clicks to get the value. When I moved the picture box out of the way I did get a value of 20. How do I make it so that the image shows and I can still get that 20 return when clicking over the picture box?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Click inside a list of points to return a number

    my code was using form mouse events.
    just change it to picturebox mouse events

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Click inside a list of points to return a number

    Quote Originally Posted by .paul. View Post
    my code was using form mouse events.
    just change it to picturebox mouse events
    Working great now. Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width