[VB.NET] Creating a PictureBox Collection

Solved
Orbital38 Posted messages 71 Registration date   Status Member Last intervention   -  
Orbital38 Posted messages 71 Registration date   Status Member Last intervention   -
Hello,

I wanted to know how to create a collection of PictureBox.
Actually, I'm trying to retrieve the first item of my collection (a PictureBox) in order to change its image. However, I'm encountering late binding errors since a standard collection is of type Object.

Currently, I'm doing this:
Public Collect As Collection Public Pic_ppl As Classe1

Collect = New Collection

I'm creating my PictureBoxes and each time I do:
Pic_ppl = New Classe1 Pic_ppl.btn = PictureBoxCs Collect.Add(Pic_ppl.btn)

Later in the code, I want to initialize an image (the first one in my collection). I've resorted to using the Name property to hack something together:
For Each Ctrl As System.Windows.Forms.PictureBox In Collect_menu.OfType(Of PictureBox).Where(Function(picBox) picBox.Name = "Pic1") Ctrl.Image = Outils.ImageList1.Images.Item(0) Next Ctrl

But I find it ugly and I would like to be able to do something like:
Collect.Item(1).Image = ImageList1.Images.Item(0)

But here, VS gives me a collection of Object with Collect_menu. I wanted to know if we can define a type for a collection and what the syntax for that would be.

Or if there's something else for a collection of PictureBox, like ImageList for a collection of Images.

Thank you! =)

Configuration: Windows / Firefox 40.0

1 answer

  1. NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
     
    Use a generic list instead:
    List(of PictureBox)
    Or if you prefer to use a collection:
    DirectCast(MyCol(5), PictureBox)

    --
    I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
    In VB.NET, remember to enable "Option Explicit" and "Option Strict"
    0
    1. Orbital38 Posted messages 71 Registration date   Status Member Last intervention  
       
      Perfect walk. ^^"

      Well, sorry for bringing up subjects for nothing like that. :/

      Just for my knowledge, what is the difference between a collection and a list? Is it the key indexing? Or does the collection offer something more?

      Thank you anyway. ^^
      0
    2. NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
       
      The collection is a list of objects, a "generic list" is a list of strongly typed objects.
      The "collection" of the framework is a simplified declaration (not exact, but that's the idea) List(Of Object)
      Otherwise, in the Collection.Generic namespace, you also have dictionaries, sorted lists, stacks, queues, ...
      0
    3. Orbital38 Posted messages 71 Registration date   Status Member Last intervention  
       
      Alright, thanks for the explanations.
      So the collections are more general to avoid being limited and have plenty of associated methods. It’s the kind of thing that can confuse someone like me who doesn’t know anything about it. Got it!

      Thanks anyway. My Form is taking shape!
      0