hi,

I have a question related to picturebox in vb.net.

I am drawing a few geometrical figures like rectangle or square or circle
on the picture box. when the user clicks on any of these figures , is there
any way to know the figure he has clicked.

Based on this i have to perform few events like , if the user
clicks on the circle in picture box then i have to increase the area of the
circle.

So can you please let me know how to perform this.

thanks and regards
dini

Rather than a picturebox try a panel.
Create a class and call it Circ.

Imports System.Drawing.Drawing2D

Public Class Circ
    Inherits UserControl

    Private mLineColor As Color
    Public Property LineColor() As Color
        Get
            Return mLineColor
        End Get
        Set(ByVal value As Color)
            mLineColor = value
            Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pevent)
        Dim pencile As New Pen(mLineColor, 1)
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        Using g As Graphics = Me.CreateGraphics
            Using gp As New GraphicsPath
                Using reg As New Region
                    gp.AddEllipse(rect)
                    Me.Region = New Region(gp)
                End Using
            End Using
            Using gp As New GraphicsPath
                rect.Inflate(-1, -1)
                gp.AddEllipse(rect)
                g.DrawPath(pencile, gp)
            End Using
        End Using
    End Sub

End Class

Now create a form and put a panel on it and use this code:

Public Class Form1
    Dim circle As Circ

    Private Sub CircleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim c As Circ = sender
        MessageBox.Show(c.Name)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Static rNum As New Random

        circle = New Circ
        circle.Name = "RedCircle"
        circle.LineColor = Color.Red
        circle.Location = New Point(10, 10)
        AddHandler circle.Click, AddressOf CircleClick
        Me.Panel1.Controls.Add(circle)

        circle = New Circ
        circle.Name = "BlueCircle"
        circle.LineColor = Color.Blue
        circle.Location = New Point(50, 50)
        AddHandler circle.Click, AddressOf CircleClick
        Me.Panel1.Controls.Add(circle)

        circle = New Circ
        circle.Name = "GreenCircle"
        circle.LineColor = Color.Green
        circle.Location = New Point(150, 150)
        AddHandler circle.Click, AddressOf CircleClick
        Me.Panel1.Controls.Add(circle)

        Invalidate()
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.