Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Event Handler to a Dynamic PictureBox

Posted on 2004-09-27
4
Medium Priority
?
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

597 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