Solved

Event Handler to a Dynamic PictureBox

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 40
Web Form VB.Net  import CSV 4 39
VB.Net Report Printing Issue 3 35
VB.Net Tranfer data between to VB.Net apps 8 26
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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