Murray Brown
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
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
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
http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.checkedindices(VS.80).aspx
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
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.Checked Items
sCheckedItems += i.ToString + Constants.vbCrLf
Next
MessageBox.Show(sCheckedIt ems)
End Sub
End Class
And then use split function to convert this back into an array
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.Checked
sCheckedItems += i.ToString + Constants.vbCrLf
Next
MessageBox.Show(sCheckedIt
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Thank you all for the information.
TheLearnedOne was spot on.
Thanks very much Bob!
Thank you all for the information.
TheLearnedOne was spot on.
Thanks very much Bob!
Bob