Results 1 to 8 of 8

Thread: How to dynamically add checkboxes in a foreach loop

  1. #1
    borris83 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2009
    Posts
    23
    Reputation
    69

    How to dynamically add checkboxes in a foreach loop

    I have the following for each loop which loops through a registry key and reads all the values:

    Code:
     
     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 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
    
                
    
            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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,944
    Reputation
    1625
    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.

  3. #3
    borris83 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2009
    Posts
    23
    Reputation
    69
    Quote Originally Posted by jmcilhinney View Post
    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:

    Code:
     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?

  4. #4
    borris83 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2009
    Posts
    23
    Reputation
    69
    I found the answer for my second question, and didn't think it was this easy:

    Code:
       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

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,944
    Reputation
    1625
    Quote Originally Posted by borris83 View Post
    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.
    Quote Originally Posted by borris83 View Post
    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.

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,944
    Reputation
    1625
    Quote Originally Posted by borris83 View Post
    I found the answer for my second question, and didn't think it was this easy:

    Code:
       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.

  7. #7
    borris83 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2009
    Posts
    23
    Reputation
    69
    Quote Originally Posted by jmcilhinney View Post
    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:
    Code:
     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
    Thanks,
    Shanmugam P
    Corp-corp.com

  8. #8
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,944
    Reputation
    1625
    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:
    Code:
    For Each item As String In chkb.CheckedItems
    In fact, in C# you MUST do that.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking