How to dynamically add checkboxes in a foreach loop

borris83

Member
Joined
Apr 30, 2009
Messages
23
Programming Experience
Beginner
I have the following for each loop which loops through a registry key and reads all the values:

VB.NET:
 Dim returnValue As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
        Dim keyname As String = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        Dim values() As String = returnValue.GetValueNames
        Dim value As String
        Dim message As String = ""




For Each value In values
            Dim readvalue As String
            readvalue = Registry.GetValue(keyname, value, Nothing)
            message &= value & " : " & readvalue & vbNewLine



          [B]  Dim chkb As New CheckBox
            Dim chkboxname As String = "chkbox" & value
            Dim text As String = value & " : " & readvalue
            Me.Controls.Add(chkb)

            chkb.Text = text
            chkb.Name = chkboxname[/B]

            

        Next

   ' for testing, comment this out
   'messagebox.show (message)

What I am trying to have been highlighted in bold.. Each time the loop executes and reads a value from the registry, I want the code to create a checkbox which has a unique name and text according to the value that was read... So the number of checkboxes will depend on number of values that is got from the registry...

But this doesn't work. It only creates one text box and the text of the textbox is also incomplete...

Can u please tell me what is wrong?
 
It is creating all the CheckBoxes. You're just not setting their Size or Location so they may not be wide enough to show all the text and they will all be sitting on top of each other. You'll need to at least set the Top property of each control to a different value so that they form a list.

That said, I'd suggest that you just use a CheckedListBox for this, rather than individual CheckBoxes.
 
It is creating all the CheckBoxes. You're just not setting their Size or Location so they may not be wide enough to show all the text and they will all be sitting on top of each other. You'll need to at least set the Top property of each control to a different value so that they form a list.

That said, I'd suggest that you just use a CheckedListBox for this, rather than individual CheckBoxes.

Thank you so much... I created a checkedlistbox with name 'chkb' and set the 'visible' to false. Then modified the code as below:

VB.NET:
 Dim returnValue As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
        Dim keyname As String = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        Dim values() As String = returnValue.GetValueNames
        Dim value As String
        Dim message As String = ""



        For Each value In values

            Dim readvalue As String
            readvalue = Registry.GetValue(keyname, value, Nothing)
            'message &= value & " : " & readvalue & vbNewLine



            Dim itmvalue As String = value & " : " & readvalue

            chkb.Items.Add(itmvalue, 0)


        Next
        chkb.Show()


This works fine. I have two questions...

1) When I run the program, I have to click on each item in checkedlistbox twice to get it checked... Is it normal?

2) Once the checkedlistbox is visible, I want to loop through each and every item in the listbox and do something according to the text which is in the item

How can I loop through it?
 
I found the answer for my second question, and didn't think it was this easy:

VB.NET:
   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For Each i In chkb.CheckedIndices

            MessageBox.Show(chkb.Items(i))
        Next

    End Sub

But still I have problem with the checkboxes in the checkedlistbox.. I have to click twice..

Pls let me know how to resolve this
 
1) When I run the program, I have to click on each item in checkedlistbox twice to get it checked... Is it normal?
Yes, that's normal. In a CheckedListBox you can select items, which highlights them just like in a regular ListBox, and you can also check them. That default behaviour allows you to select an item without checking it. If you don't want that behaviour then set the CheckOnClick property to True and the items will be checked with the first click.
2) Once the checkedlistbox is visible, I want to loop through each and every item in the listbox and do something according to the text which is in the item

How can I loop through it?
The CheckedListBox has an Items collection and a SelectedItems collection, just like a regular ListBox. It also has a CheckedItems collection. You can use a For or For Each loop with any of them.
 
I found the answer for my second question, and didn't think it was this easy:

VB.NET:
   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For Each i In chkb.CheckedIndices

            MessageBox.Show(chkb.Items(i))
        Next

    End Sub

But still I have problem with the checkboxes in the checkedlistbox.. I have to click twice..

Pls let me know how to resolve this
Unless you actually need the index for something other than getting the item, just loop through the SelectedItems collection directly rather than looping through SelectedIndices.
 
Unless you actually need the index for something other than getting the item, just loop through the SelectedItems collection directly rather than looping through SelectedIndices.

Thanks..
I simplified the code:
VB.NET:
 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim item As String
        For Each item In chkb.CheckedItems

            MessageBox.Show(item)
        Next

    End Sub


and set the 'checkonclick' to true
 
It's a small thing but, unless you need to use it outside the loop, it's considered better practice to declare the loop control variable on the loop, not outside:
VB.NET:
For Each item As String In chkb.CheckedItems
In fact, in C# you MUST do that.
 
Back
Top