Hi,

i am creating a project and i am having a problem

i have created an image in a rectangle using the following code:

Dim d As New Rectangle(Width - 127, 8, 22, 15)
        e.graphics.DrawImage(My.Resources.close, d.Location)

now the problem is that if the user click on the image, i want form to close

i dont know how to get the click event handler for the image

Recommended Answers

All 3 Replies

Hi
This is VB.NET?
> First preserve the value of d (Rectangle) for class (Form) level
> U are not specify where u draw image (Form, PictureBox etc..) I assume Form
> In the Form_MouseDown () check whether the user clicked inside the rectangle using d.Contains(). If it is inside the do the appropriate action.

Code

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If d.Contains(e.Location) Then
            ' User Clicked the Image
            Me.Dispose()
        End If
    End Sub

Hi,
I think u using Dot Net 2003, but i am given code in Dot NET 2005.

Dim MyRect As Rectangle
    Dim MyImage As image
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        MyRect = New Rectangle(100, 100, 40, 40)
        MyImage = Image.FromFile("C:\WINDOWS\Web\Bullet.gif")

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawImage(MyImage, MyRect)
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If (MyRect.Contains(e.X, e.Y)) Then
            Me.Dispose()
        End If
    End Sub

The above code working fine in VB.Net 2003 and 2005. Make sure that the file ("C:\WINDOWS\Web\Bullet.gif") is existing in your system.

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.