I said this before: "I like Arraylists. They are wonderful. You can feed them anything and they will digest it without complaining. You can add, delete, insert elements or ranges of elements without any complaint and without even needing to re-dimension them. They take objects, and the objects can be of any class, images, sounds, controls, filenames..., even you can add different kinds of objects one after the other or at least a refference to it"
But another D.I.C. member told me: "It's not a "great" thing that the ArrayList doesn't do any type of type checking. If you were stuck in .Net 1.1, then yes, ArrayList is all you have. But once .Net 2.0 was released(almost 7 years ago), there has been better alternatives to the ArrayList."
I had seen a couple of times the List(Of T) on some post, but I always overlooked them. Well, Stubborn as I am.
Now I have this small project that I made just to show you something that may be useful, simulate a control array.
Here are a set of 10 labels with sequential names as they are created when you drop them down on the form from the toolbox. A button to start the operation and a label to give you some status information about the other labels. It's name has been changed in order of not
be included in the controlarray.
This is indeed a simple program with only two subroutines, "Form1_Load" and "Button1_Click". The "Imports System.Threading" and the "Refresh" statements are just to have access to a delay on the animation, so it will happen at a rate that allows you to see it happening. The "System.Collections" is for Lists.
Imports System.Threading Imports System.Collections Public Class Form1 Dim Labels As New List(Of Label) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Labels.Clear() For Each cnt As Object In Me.Controls If cnt.Name.Contains("Label") Then Labels.Add(cnt) End If Next Labels.Sort() End Sub
We create a List to contain the controls, in this case the labels from "Label1" to "Label10" and just for safety when we start, we clear it. Then we load the controls that have a property in common, in this case the prefix "Label".
From here we could use the suffix as an index if we parse the name and convert the number part to integer and then subtract one from this number, but we are not doing this in this case. We are storing the controls themselves. So we add them to the List in a for
loop.The "Sort" statement is appropriate here because labels have a name property that is text. It is not strictly necessary here because on purpose we kept the sequential names.
Now they are objects contained in the list. Because they have properties, we can access them by property as name, size, left, top, etc. But we will access them in this case by index in the list.
The "Button1_Click" handler starts a sequence where we do different thing with the control in the list.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num As Integer For j = 0 To 8 For i = 0 To Labels.Count - 1 num = Val(Labels(i).Name.Substring(5)) If num Mod 2 = 0 Then Labels(i).Top = Labels(i).Top + 15 lblInfo.Text = Labels(i).Name & " is at X=" & Labels(i).Left.ToString & " and Y=" & Labels(i).Top.ToString Refresh() Thread.Sleep(150) Else Labels(i).Left = Labels(i).Left + 15 lblInfo.Text = Labels(i).Name & " is at X=" & Labels(i).Left.ToString & " and Y=" & Labels(i).Top.ToString Refresh() Thread.Sleep(150) End If Select Case num Mod 3 Case 0 Labels(i).BackColor = Color.Bisque Case 1 Labels(i).BackColor = Color.Magenta Case 2 Labels(i).BackColor = Color.Lime End Select If i = Labels.Count Then MessageBox.Show("You might want to press Button1 Again") Thread.Sleep(150) Application.DoEvents() Next Next End Sub
We change the background color, the left and top properties using the index of the list. Nothing fancy, but may be useful if you need a control array.
Be patient when you start the sequence, it only shifts the labels 8 times and there is the "Application.DoEvents()" that will keep the system responsive.
Please check the attached project.
Number of downloads: 330