locked
Simpler way to do this? RRS feed

  • Question

  • Hey guys.

    I have this code :

     Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick

            If c0lor.ShowDialog = Windows.Forms.DialogResult.OK Then
               PictureBox1.BackColor = c0lor.Color
            End

     End Sub

    I could do it 16 times, but that is wayy to inconvenient and hard to read.


    Is there any way to do it shorter? Something like this: (It's pseudo code.)

     
     Private Sub PictureBoxes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles  PictureBox1.DoubleClick, PictureBox2.DoubleClick, PictureBox3.DoubleClick, PictureBox4.DoubleClick, _
        PictureBox5.DoubleClick, PictureBox6.DoubleClick, PictureBox7.DoubleClick, PictureBox8.DoubleClick, _
        PictureBox9.DoubleClick, PictureBox10.DoubleClick, PictureBox11.DoubleClick, PictureBox12.DoubleClick, _
        PictureBox13.DoubleClick, PictureBox14.DoubleClick, PictureBox15.DoubleClick, PictureBox16.DoubleClick

    'Do something to the individual picturebox, rather than all 16.

        End Sub

    • Edited by NolanLi Monday, August 3, 2009 11:07 PM
    Monday, August 3, 2009 11:03 PM

Answers

  • Try this, if you have your picturebox directly on the form, this code will work but if your pictureboxes are on a panel then change Me.controls to Panel1.control or the name of your panel.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load                
            For Each cont As Control In Me.Controls
                If cont.GetType.Name = "PictureBox" Then
                    AddHandler cont.DoubleClick, AddressOf PictureBoxes_DoubleClick
                End If
            Next   
        End Sub
    
        Private Sub PictureBoxes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim picB As PictureBox = CType(sender, PictureBox)
            MsgBox(picB.Name.ToString) 'this just for test
          'add whatever you want  any pictureBox you doubleclick to do here
          
        End Sub
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Marked as answer by NolanLi Tuesday, August 4, 2009 3:17 AM
    Tuesday, August 4, 2009 3:16 AM

All replies

  • There are a couple ways to do this, you can find out the picture box by using sender.Equals(PictureBox5) then you can cast the sender to a picture box. Just one way.
    http://blogs.windowsclient.net/joes
    Monday, August 3, 2009 11:53 PM
  • Try this, if you have your picturebox directly on the form, this code will work but if your pictureboxes are on a panel then change Me.controls to Panel1.control or the name of your panel.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load                
            For Each cont As Control In Me.Controls
                If cont.GetType.Name = "PictureBox" Then
                    AddHandler cont.DoubleClick, AddressOf PictureBoxes_DoubleClick
                End If
            Next   
        End Sub
    
        Private Sub PictureBoxes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim picB As PictureBox = CType(sender, PictureBox)
            MsgBox(picB.Name.ToString) 'this just for test
          'add whatever you want  any pictureBox you doubleclick to do here
          
        End Sub
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Marked as answer by NolanLi Tuesday, August 4, 2009 3:17 AM
    Tuesday, August 4, 2009 3:16 AM
  • :D THank you!
    Tuesday, August 4, 2009 3:17 AM