VB.Net == DrawImage(), A way to make them selectable?

The simple coding I have is something like this:

    Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim x As Integer = 10, y As Integer = 10
        For i = 0 To 10
            e.Graphics.DrawImage(Form1.Images(i), x, y, 17, 17)
            y += 24
    End Sub

Images(i) is a group of images (for a minimap): Public Images(10) As System.Drawing.Bitmap

The thing is, I want the images to be 'selectable' via mouse.. Is there a possible way to do this? The reason for this is that they can select a different minimap image to use, depending on what image they actually click.

So what I am asking is if there is any way to detect selecting via mouseclick (hitbox), and if not, what other ways can I go around doing this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
In the MouseDown() event you are supplied the coordinates of the click.  Just do the math to figure out which image was clicked...
Normally this is done by handling mouse down events.  Your form will have mouse button or mouse click events which receive an X and Y position.  You use these positions to decide which image was clicked.

The code shown draws the images as a column, placed at [10, 10], with 17 pixel square images, spaced at 24 pixels intervals. So calculating which image was clicked given an X and Y coordinate would work like this.

Dim selected As Integer

If (x > 10) And (x < 27)
    if (y > 10)
        selected = (y - 10) / 24
        y = y - (selected * 24)

        if (y < 17) And (selected < 10)
              // selected is the index of the image that the user clicked

// if any of the If tests are False, the user did not click over an image, just leave the selection where it is

Open in new window

You need to display the selection in your paint function, perhaps by drawing a coloured rectangle behind the selected image.  When the user clicks on a new image, the program needs to redraw the form to show the new selection.

Forgive any syntax errors, its so long since I used VB that I can't remember the proper syntax.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
I would say that an easy way of achieving this, is to have a list of objects, that represent the bounds, and the image, and then you can use the Contains(point) method to check if the Rectangle contains the point clicked.  If you have LINQ, then you can use a simple lambda expression.

Proof-of-concept (untested):

Private Class ImageObject
    Public Property Image As Image
    Public Property Bounds As Rectangle
End Class

Private m_imageList As New List(Of ImageObject)

Dim selectedImage As ImageObject = m_imageList.Where(Func(x) x.Bounds.Contains(New Point(e.X, e.Y)).FirstOrDefault()

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you're gonna go that far then you might as well add a Paint() method that receives a Graphics and store the "selected" state inside the ImageObject class.  =)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.