[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 -
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:
I'm creating my PictureBoxes and each time I do:
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:
But I find it ugly and I would like to be able to do something like:
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
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
Related links:
- [VB.NET] Select entire line ListView
- changing language in vb.net
- Retrieve the result of a SELECT (VB.net)
- VB.net program does not start
- Éteindre le PC avec VB.NET peut être effectué en utilisant la commande suivante : ```vb Process.Start("shutdown", "/s /t 0") ```
- Connecting to MySQL from VB.net version 8.0.28
1 answer
-
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"-
-
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, ... -
-