ContainerControl Array

Hi Everyone.

I Am a little stuck creating a ContainerControl Array. I Want to create multiple instances of a ContainerControl block dynamically.

I can create multiple instances by repeating the code but I don’t know how many instances I will need at different states of execution.

  Dim buttonContainer2 As new ContainerControl1
  buttonContainer2.EmbedWithin(Self,10,72,buttonContainer2.Width,buttonContainer2.Height)
  
  Dim buttonContainer3 As new ContainerControl1
  buttonContainer3.EmbedWithin(Self,10,132,buttonContainer3.Width,buttonContainer3.Height)

If I attempt to do an Array: (it fails with an OutOfBoundsException)

  Dim buttonContainer1() As ContainerControl1
  buttonContainer1(0).EmbedWithin(Self,10,10,buttonContainer1(0).Width,buttonContainer1(0).Height)

Any advice would be appreciated.

You need to append a container control to the array. Right now there is just an empty array
Dim cc() As containercontrol1
Dim myCC as New containercontrol1
cc.Append myCC

Thanks for the advice. When I run the new code it only creates the first instance.

  Dim bcA() As ContainerControl1
  Dim bcI As New ContainerControl1
  
  bcA.append bcI
  bcA(0).EmbedWithin(Self,10,10,bcI.Width,bcI.Height) 'The only instance created on the page
  
  bcA.append bcI
  bcA(1).EmbedWithin(Self,10,72,bcI.Width,bcI.Height)
  
  bcA.append bcI
  bcA(2).EmbedWithin(Self,10,134,bcI.Width,bcI.Height)

Event though you’ve appended it to the array 3 times, you’ve only created one instance. You’ll need to create 3 separate instances.

Thanks Paul. Is there no other way you can do this? If I want 20 instances maximum I must create it 20 times?

  Dim bcA() As ContainerControl1
  Dim bcI As New ContainerControl1
  Dim bcI2 As New ContainerControl1
  Dim bcI3 As New ContainerControl1
  
  bcA.append bcI
  bcA(0).EmbedWithin(Self,10,10,bcI.Width,bcI.Height)
  
  bcA.append bcI2
  bcA(1).EmbedWithin(Self,10,72,bcI.Width,bcI.Height)
  
  bcA.append bcI3
  bcA(2).EmbedWithin(Self,10,134,bcI.Width,bcI.Height)

Just loop
While Ubound(bca) < 19
Dim b As new cc
bca.Append b
Loop

Sure, but you don’t have to create them separately like that. A For loop is a good way to reduce repeating code:

Dim containers As ContainerControl1 For i As Integer = 0 To 19 Dim c As New ContainerControl1 c.EmbedWith(Self, 10, i * c.Height + 10, c.Width, c.Height) containers.Append(c) Next

@Paul Lefebvre and @Jym Morton … Thanks for your input. Much appreciated. :slight_smile:

For the sake of completion. Here is my final piece of experimental code.

  Dim containers() As ContainerControl1
  
  for i as integer = 0 to 19
    Dim c As New ContainerControl1
    c.EmbedWithin(Self,10,i*c.Height+20,c.Width,c.Height)
    containers.Append(c)
    
    'Set properties
    c.id = i
    
    Select Case i
    Case 0
      c.shellcmd = "dir"
    Case 1
      c.shellcmd = "dir/w"
    Case 2
      c.shellcmd = "ipconfig"
    Case 3
      c.shellcmd = "ver"
    Case 4
      c.shellcmd = "time/t"
    Case 5
      c.shellcmd = "date/t"
    Case 6
      c.shellcmd = "wmic cpu get name"
    Case 7
      c.shellcmd = "regedit"
    Case 8
      c.shellcmd = "ver"
    Case 9
      c.shellcmd = "ipconfig"
    Else
      c.shellcmd = "dir"
    End Select
    
    c.btnCommand.Caption = c.shellcmd
    
  Next

I’d make a string array of your text first then just use i to access it
Dim myShells() as String = Array(“dir”, “dir/w” … )
for i As integer = 0 to 19
Dim c As New ContainerControl1
c.Shellcmd = myShells(i)
c.btnCommand.Caption = c.shellcmd
c.EmbedWithin(Self,10,i*c.Height+20,c.Width,c.Height)
containers.Append©
Next i

@Jym Morton Thanks. It works like charm :wink:

I Just modified the FOR statement to count up to the last entry in the Array.

  Dim containers() As ContainerControl1
  Dim shellCommands() As String = Array("dir","dir/w","ipconfig","ver","time/t","date/t","wmic cpu get name")
  
  for i as integer = 0 to shellCommands.Ubound
    
    Dim c As New ContainerControl1
    c.EmbedWithin(Self,10,i*c.Height+20,c.Width,c.Height)
    containers.Append(c)
    
    c.id = i
    c.shellcmd = shellCommands(i)
    c.btnCommand.Caption = shellCommands(i)
    
  Next

A good habit is to assign upper limit of the FOR to a variable because sometimes there’s math and you don’t want to calculate it on every loop
dim UpperLimit As Integer = shellcommands.Ubound
Like in a listbox upperlimit would be (ListCount - 1) I don’t know if it needs to calculate Ubound each time or if it becomes a static property of the array.
for i As Integer = 0 to UpperLimit

I subclass a containerControl and call it containerlist, I add a few methods to add a container to the list/remove etc… and it does all the work of placing the containers at the proper height etc… It might be a helpful design pattern for you

Sounds interesting. Thanks :slight_smile: