• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

VB.net/2005 CheckedListBox Gather CheckItemCollection into an array that array into a string

Hi

I have a CheckedListBox on a windows form in Visual Studio 2005.

How do you gather the checked items into an array?

Furthermore can this array be stored as a string variable (My app writes to a text file using string arrays)

Thank you
0
Murray Brown
Asked:
Murray Brown
1 Solution
 
Bob LearnedCommented:
Are you writing XML files or straight text files?

Bob
0
 
protenCommented:
You can pick off the checked items and add them to your string array.

http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.checkedindices(VS.80).aspx
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Bob

I am writing straight text files.

I need to gather which items are checked and load them into the String array that is stored in text files and then used at runtime.

At runtime the code executes based on an array value. So the string value needs to be converted back to an array.

I suppose that I could gather the checked items into a string (with the items separated by a comma) and then use the split function
to convert this back into an array.

Thanks

Murray
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
maidinhtaiCommented:
Try this example:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sCheckedItems As String = ""
        For Each i As Object In Me.CheckedListBox1.CheckedItems
            sCheckedItems += i.ToString + Constants.vbCrLf
        Next
        MessageBox.Show(sCheckedItems)
    End Sub
End Class
And then use split function to convert this back into an array
0
 
doraiswamyCommented:
try this

Public Class Form1
    Dim selitem() As String

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer = 0
        For i = 0 To clb.Items.Count - 1
            If clb.GetItemChecked(i) Then
                If j = 0 Then
                    ReDim selitem(0)
                Else
                    ReDim Preserve selitem(j)
                End If
                selitem(j) = clb.Items.Item(i).ToString
            End If
        Next

    End Sub
End Class
0
 
Bob LearnedCommented:
Murray,

Can you do something like this?

  Dim list(Me.CheckedListBox1.CheckedItems.Count - 1) As String
  Me.CheckedListBox1.CheckedItems.CopyTo(list, 0)
  Dim joined As String = String.Join(",", list)


Bob
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

Thank you all for the information.

TheLearnedOne was spot on.

Thanks very much Bob!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now