0

My program creates an array of checkboxes at runtime as shown below:

For Looper = 0 To 36
  Dim Ex1ConfigCheck As New CheckBox                    
  frmSetup.Controls.Add(Ex1ConfigCheck)                ' Add Control to from
  Ex1ConfigCheck.Top = (Looper + 45) + (Looper * 18)   ' Set Location
  Ex1ConfigCheck.Left = 210
  Ex1ConfigCheck.Text = Setup.ExCheckName(Looper)      ' Set Text property from strArray
Next

This is where I don't know how to proceed.

I would like to fill a boolean array (ex. MyBoolean(37)) with the value of Ex1configCheck().Checked. The reason I would like to fill another array is because I need to be able to reference the value of the checkboxes in other parts of the code but can't access them until they are created. Also, I plan on saving the array out to a binary file.

Could someone point me in the right direction please?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Opie
  • 1
  • 1
    Add a `List(Of CheckBox)` field and add elements to it while looping. I suggest to use a FlowLayoutPanel or a TableLayoutPanel to host your controls, so you don't need to provide (wrong) values to place them. – Jimi Apr 08 '20 at 20:09
  • @Jimi The OP might be using wpf where those panel don't exist. However, there are similar panels in wpf where the need to specify location would not be necessary. – SezMe Apr 09 '20 at 04:38
  • @SezMe Maybe. What's less usual in WPF is something named `frmSetup`. But `frmSetup.Controls.Add()` kind of gives it away. – Jimi Apr 09 '20 at 04:40

2 Answers2

0

If there are no other CheckBoxes in the same container as those ones then you can do this:

Dim flags = Me.Controls.OfType(Of CheckBox)().
                        Select(Function(cb) cb.Checked).
                        ToArray()

If the controls are in a different container than the form itself, replace Me with that container.

As suggested by @Jimi, you could also create a List(Of CheckBox) and assign that to a field, populating it when you create the controls. You can then use that list instead of creating one on demand:

Dim flags = myCheckBoxList.Select(Function(cb) cb.Checked).
                           ToArray()

Of course, if you know exactly how many CheckBoxes you are going to be adding, why do you need to wait until run time to create them? Why can't you create them at design time and then modify them at run time? You usually only create controls at run time if you don't know how many there will be until run time, but that seems not to be the case here.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thank you for the comments. I was planning on having 8 groups of 37 checkboxes so the number was significant. Creating this many at design time was not something I was looking forward to do. For what it's worth, I have found an alternative solution to my problem and I have outlined it in my "Answer" post... – Opie Apr 09 '20 at 12:40
0

Thanks all for your answers and comments. I always have a fear of being roasted when I ask what some may consider a simple question online.

I have found an alternative way of accomplishing my task. Instead of creating 8 "Arrays" of checkboxes, I have learned of a very simple control available called "CheckedListBox".

I really didn't need to create the checkboxes at runtime but was trying to find an easier way to create 8 groups of 37 checkboxes without having to manually name and set the properties of each one during design. I also wanted to be able to index them in my code to be able to update and read the value using simple loops. I could have done this by creating arrays of CheckBox but again, I would have had to manually initialize the arrays.

Once I found the CheckedListBox, I was able to accomplish what I want very quickly. I only had to set the properties of the 8 "groups" (CheckedListBox's) and fill them using the items property. The ListBox essentially created a List like Jimi suggested automatically and I can index thru each list with a loop as desired. Jimi's suggestion actually lead me to finding the CheckedListBox while I was searching for more information on using "List(of CheckBox)".

Sometimes talking to others helps me find the right questions to ask. Google was able to figure out what I wanted when I searched for "List(of CheckBox)". (:

Opie
  • 1