Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you writing XML files or straight text files?

Bob
Avatar of proten
proten

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
Avatar of Murray Brown

ASKER

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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

Thank you all for the information.

TheLearnedOne was spot on.

Thanks very much Bob!