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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

Thank you all for the information.

TheLearnedOne was spot on.

Thanks very much Bob!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.