Solved

Event Handler to a Dynamic PictureBox

Posted on 2004-09-27
4
717 Views
Last Modified: 2012-08-13
I am creating a new PictureBox with an event Handler.  See code.  This code works fine.  It displays a small image on top of the large image.  What I want to do now is close the small image (or set it's visibility = false) when I click on the small image.  I guess the main question is how to Add an event Handler to an object that's created dynamically.

Private Sub LargeImg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LargeImg.MouseDown
        If e.Button = MouseButtons.Left Then
            Dim myPB As PictureBox = New PictureBox
            myPB.Image = Image.FromFile("c:/Project/Images/SmallImg.jpg")
            myPB.SetBounds(e.X, e.Y, 20, 16)
            myPB.Name = "SmallImage"
            myPB.Parent = LargeImg
            myPB.Show()
        End If
End Sub
0
Comment
Question by:jason_allenberg
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12164835
This add a handler dinamically

AddHandler myPB.Click, AddressOf myPB_Click


Private Sub LargeImg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LargeImg.MouseDown
        If e.Button = MouseButtons.Left Then
            Dim myPB As PictureBox = New PictureBox
            myPB.Image = Image.FromFile("c:/Project/Images/SmallImg.jpg")
            myPB.SetBounds(e.X, e.Y, 20, 16)
            myPB.Name = "SmallImage"
            myPB.Parent = LargeImg
            AddHandler myPB.Click, AddressOf myPB_Click
            myPB.Show()
        End If
End Sub

Public Sub myPB_Click(ByVal sender As Object, ByVal e As System.EventArgs)
     myPB.Visible = false
End Sub
0
 

Author Comment

by:jason_allenberg
ID: 12169398
Thanks.  That works as long as I declare myPB outside the Private Sub.  myPB needs to be Public for myPB_Click to reconize it.

I need to take this code one step forward.  I have an large PictureBox on the form.  When a user clicks on the large PictureBox, a new myPB is created.  So you can have multiple PictureBoxes created.  Right now, the Envent Handler will only close the most recent PictureBox.  However I want to be able to close any of them, by clicking on the one I want to close.  Can I use myPB.Name and change the name for each one?  I've been playing around with the code, but can't put it together.

Thanks.  
0
 
LVL 8

Accepted Solution

by:
wguerram earned 300 total points
ID: 12169737
Then use the sender paramenter.

it's the object on which the click was performed.

Public Sub myPB_Click(ByVal sender As Object, ByVal e As System.EventArgs)  
     sender.Visible = false
End Sub

if you have problems let me know it.
0
 

Author Comment

by:jason_allenberg
ID: 12170022
Perfect. Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now