Page 1 of 1

Using a List(Of T) to simulate a ControlArray Rate Topic: -----

#1 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 18 February 2012 - 04:50 PM

Using Lists To Simulate Control Array.

Hello!

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.

Attached Image


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.

CtlArray.Add(cnt) 




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.

ricardosms.

Attached File(s)



Is This A Good Question/Topic? 1
  • +

Replies To: Using a List(Of T) to simulate a ControlArray

#2 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 154
  • Joined: 22-September 09

Posted 15 May 2012 - 11:12 AM

Great article, thank you.

Just a note... When running the code it halts at
Labels.Sort()


with an error stating that two elements in a matrix cannot be compared (Invalid Operation Exception).
What is causing it?

Also, the msg will not show up unless it's modified to:
If j = 6 And i = 9 Then MessageBox.Show("You might want to press Button1 Again")


Was This Post Helpful? 0
  • +
  • -

#3 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 15 May 2012 - 03:53 PM

Some objects don't have a text or name property that can be compared. I used sort because I am loading the labels from the control's collection and the tab order may not be the same than the alphabetic order. If you don't sort the list, the element will remain in the order you added them.

My copy doesn't halt.
Are you using labels? Are you running my program or did you make one similar? Is there any message?


For the messagebox, I moved it out of the loops.
Was This Post Helpful? 0
  • +
  • -

#4 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 154
  • Joined: 22-September 09

Posted 16 May 2012 - 03:07 AM

Thanks for following up.

I created my own code following your article but because of the errors (actually two, the first one forced me to turn off “Option Strict” for
If cnt.Name.Contains("Label") Then
                Labels.Add(cnt)
End If 
), I downloaded yours and ran it as-is. Your code works fine once commented
'Labels.Sort()


Because I’m not an expert (just a hobbyist), I'm trying to understand why errors happen :online2long:
Was This Post Helpful? 0
  • +
  • -

#5 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 16 May 2012 - 09:23 AM

Option Strict forces you to follow standard guidelines. The compiler is slacker when you don't have it. Warnings become errors when using strict on, because the compiler won't assume things and won't do some conversions. I am a hobbyist too. I post my code on the hope of getting feedback, but it won't happen very often.

The sorting is not strictly necessary, I put it there for security, but my code runs fine on my system both ways, and it went through the scrutiny of a moderator when the tutorial was appoved.

Thank you for your comments and please keep posting, this is constructive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1