[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 663
  • Last Modified:

how do i pass a variable through "add handler"?

im a complete amatuer compared to most people on this site, firstly i dont want somone to do my work for me (im a univeristy student) but i have an assignment to create a photo album of some sort. The problem that i have faced is that im dynamically creating a picture box for each image which is located in a selected directory. So far i have managed to display kinda like a thumbnail version of all the images in this selected directory, but the issue arised when i wanted to show a larger version of one of these thumbnails when the user clicked on it. Using the "add handler" i can create a custom event to emulate this but what i really need to do is to pass the picturebox.image data from the "add handler" part to the custom event sub routine. i hope this makes sense, sorry about all the writing. Thanks
Imports System.IO
 
Public Class main
 
    Private Sub UploadPhotosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadPhotosToolStripMenuItem.Click
 
        FolderBrowser.ShowDialog()
        Dim fileList() As String = Directory.GetFiles(FolderBrowser.SelectedPath, "*.jpg", SearchOption.AllDirectories)
        Dim objFile As String
 
        Try
            For Each objFile In fileList
 
 
                Dim newPictureBox As PictureBox = New PictureBox
                Dim num = fileList.GetUpperBound(0)
 
                'dynamically creating pictureboxes for thumbnail preview
                newPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
                newPictureBox.Size = New System.Drawing.Size(100, 100)
                newPictureBox.Image = System.Drawing.Image.FromFile(objFile)
 
                Me.Controls.Add(newPictureBox)
                Flow.Controls.Add(newPictureBox)
 
                'creating custom event
                AddHandler newPictureBox.Click, AddressOf img_Click
 
                ImageListA.ImageSize = New Size(100, 100)
                ImageListA.Images.Add(Image.FromFile(objFile))
                imgProcess.Step = 100 / num
                imgProcess.PerformStep()
 
            Next
 
        Catch ex As Exception
            MsgBox("Errors encountered during IO operation")
        End Try
 
 
    End Sub
 
   
    Private Sub img_Click(ByVal sender As Object, ByVal e As EventArgs)
 
 
 
    End Sub
 
End Class

Open in new window

0
griff1988
Asked:
griff1988
  • 5
  • 4
  • 2
  • +1
1 Solution
 
abelCommented:
Your addhandler is correct. And, as it is, the sender object already is the picture object, so you can access that easily by casting it to the picturebox type:

Private Sub img_Click(ByVal sender As Object, ByVal e As EventArgs)
     Dim picBox = CType(sender, PictureBox)

     ' do something with picBox, like getting the image

End Sub


0
 
ALaRivaCommented:
Don't quite understand your question, but from what I'm *thinking* you mean, is that you might need to check the sender object of the img_Click()

You can DirectCast() or CType() to a Picturebox object, and then go from there.

Still, a little more clarification might help.

- Anthony
0
 
ALaRivaCommented:
speedy beat me to it. ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abelCommented:
hi Anthony. Apparently we were on the same page when it came to understanding the question :). Fair split?
0
 
ALaRivaCommented:
no need,  you got it clear. ;)

Just razzing the higher ups.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*** NOT FOR POINTS ***

I would just modify abel's code slightly so that Intellisense will work for....

Change:

    Dim picBox = CType(sender, PictureBox)

To:

    Dim picBox As PictureBox = CType(sender, PictureBox)
0
 
abelCommented:
Thanks, yes, you're right. And actually, as a result of some other thread these days, I was wondering whether the new feature, type inference, would actually work when you do not use "As SomeType". And though type inference is a compile time feature, I thought it was time to find out whether Microsoft's intellisense also picked up on this new treat.

And, much to my surprise (honestly!), the following worked. pic2 is interpreted as a PictureBox. This, however, only works for VS 2008. Earlier versions do not support it.

ScreenShot236.png
0
 
abelCommented:
This doesn't mean that you shouldn't use "As PictureBox". It is always better to be explicit than to be implicit. It will help you later understand your code and there will be cases where it is vital to specify because type inference can also work against you (sorry for drifting a bit off topic).
0
 
ALaRivaCommented:
That is a ridiculously awesome tip there abel.  Although I don't think I'd ever break habit of explicitly setting type, it's good to know that it's there if for some reason you don't set type.

Thanks for that tip, I hadn't heard of it.
0
 
griff1988Author Commented:
thanks guys for the quick help, basically i want to be able to click on the dynamically created pictureboxes which are the thumbnails, then from there depending on which one i click on display the larger version of it in a different text box.

problem is:

AddHandler newPictureBox.Click, AddressOf img_Click (if i try and pass a variable here it comes up with error)

what im trying to pass would make the add handler snippet look something like:

AddHandler newPictureBox.Click, AddressOf img_Click(newPictureBox.image)

then the sub routine....

Private Sub img_Click(ByVal sender As Object, ByVal e As EventArgs, byVal showImage as system.drawing.image)

but trying to pass a variable through the add handler produces this error:

'AddressOf' must be the name of a method (without parenthesis)

Any ideas????

again, im a bit of an amatuer so, please bare with me. thanks.
 
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good to know!...   =)
0
 
griff1988Author Commented:
Idle_Mind, abel your guys are great thanks, worked wonders!!!!!! this is one univeristy student who is happy, makes a change lol.
0
 
ALaRivaCommented:
Look back up, the code you provided initially is more than sufficient.

You just need to use the img_Click's Sender Object.

In your img_Click event:
Dim picBox As PictureBox = CType(sender, PictureBox)

Then you can just use the picBox object (The picture box that was clicked) and do what you need to do.

- Anthony
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now