?
Solved

Event Handler to a Dynamic PictureBox

Posted on 2004-09-27
4
Medium Priority
?
731 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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