Subrutine to Handle ALL PictureBox

kimosavi

Active member
Joined
Apr 9, 2009
Messages
34
Location
Chicago, IL
Programming Experience
3-5
Hi. Have been experimenting with VB.NET and seems awesome!

I am trying to do something and need to see if there is an easier way.

I have 50 pictureboxes in one form. I need to write the same code for each one.

I understand I can do this in two ways

one is to write a function for each PictureBox handling each object induvidually

two is to write a function for all PictureBoxes that handle ALL object, but I have to declare each object in the Handles syntax of the subrutine

as follows:

VB.NET:
Private Sub PreviewTile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB1.DoubleClick, PB2.DoubleClick, PBChapel.DoubleClick
        DrawBorder(sender, Color.Wheat, 3)
        frmBoardPreview.PBPreview.Image = FindPicture("TB", sender.Tag)
        frmBoardPreview.Show()
    End Sub

I was wondering if there is a way to write a subrutine that will Handle ALL Pictureboxes without having to enumerate them.

example

VB.NET:
Private Sub PreviewTile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.PictureBoxes.DoubleClick '<-- made up
        DrawBorder(sender, Color.Wheat, 3)
        frmBoardPreview.PBPreview.Image = FindPicture("TB", sender.Tag)
        frmBoardPreview.Show()
    End Sub

is it possible, could anyone point me into the right direction

Thanks!!

Kimo
 
I have done similar stuff so lets try this, I didn't have time to test this.
Make your custom sub:
VB.NET:
Private Sub MydoubleClick(byVal sender As Object,Byval e As EventArgs)
' you code for the dbl click - pic boxes
   Dim pb As New PictureBox = CType(sender, PictureBox)
   'pb.<event>....
End Sub
' form load
For Each ctr As Control In Me.Controls
   If TypeOf ctr Is PictureBox Then
      Dim pb As PictureBox = CType(ctr, PictureBox)
      Addhandler pb.DoubleClick, AddressOf MyDoubleClick 
      'add more addhandler for more events that your need
   End If
Next
 
Last edited:
Hey newguy, Worked like a charmed. Just made a tiny little modification but was a go on the first pass.

This really helped reduce a lot of code. [kimosavi bows]

thanks man!

I am liking this VB Stuff... really powerful and flexible!
 
Back
Top