Solved

Event Handler to a Dynamic PictureBox

Posted on 2004-09-27
4
730 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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