0

I have a Listbox with 100-200 default values and the multiselect-simple mode is enabled.

I save the selected text items in a List of string:

Private Sub ListBox_Styles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox_Styles.SelectedIndexChanged

    Styles_List.Clear()

    For Each item In ListBox_Styles.SelectedItems : Styles_List.Add(item) : Next

    Save_INI_settings()

End Sub

After that the sub calls "Save_INI_settings" procedure then I save the values into a INI file like this:

WriteINIFile.WriteLine("Styles=" & String.Join(",", Styles_List.ToArray))

...Which produces this result:

Styles=Alternative,Electro,Pop,Rock

Now, in the next load of my application, how I can set the listbox selected items by picking the text values of the ini?

This is how I load my INI settings:

    If ValueName = "Styles".ToLower Then
        For Each Item In Value.split(",")
            ' ListBox_Styles.SetSelected(Item, True)
        Next
    End If

...Where "ValueName" var is "Styles" and "Value" var is "Alternative,Electro,Pop,Rock" so with the for i get this:

Alternative
Electro
Pop
Rock

PS: I don't want to save the index integers in the INI file instead the text items, also I don't want to store this settings in the app settings section.

UPDATE:

This is how I'm doing it right now:

If ValueName = "Styles".ToLower Then
   For Each Item In Value.split(",")
 ' Try to add the string as is
   ListBox_Styles.SelectedItems.Add(Item)
 ' Try to add the string as TitleCase
   ListBox_Styles.SelectedItems.Add(Char.ToUpper(Item(0)) + StrConv(Item.Substring(1), VbStrConv.Lowercase))
 ' Try to add the string as WordCase
ListBox_Styles.SelectedItems.Add(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Item))

   Next
End If

Works as expected, but too much iterations

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

1 Answers1

1

Given that your listbox contains only strings, then you could simply readd the strings to the SelectedItems collection

   .....
   For Each Item In Value.split(",")
        Listbox.SelectedItems.Add(Item)     
   Next

EDIT
Following your comment below, then I can suggest to look at the System.Globalization namespace. Here you could find the method TextInfo.ToTitleCase that converts to a Proper case a given string

   Imports System.Globalization

   Dim ti = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo
   For Each Item In Value.split(",")
      Listbox.SelectedItems.Add(to.ToTitleCase(Item))
   Next
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks for your answer, but it fails if any value is lowerstring and the listbox item is Wordcase, for example INI value: "rock" and Listbox item value: "Rock", you know how I can do it in that circunstances? a Ignorecase or something? – ElektroStudios May 22 '13 at 15:48
  • Well, what is the reason for the case change? If it is possible I would not change the case when writing the INI file, otherwise, there is in the Globalization namespace a method that restore the proper case of a string. Let me check and I will update the answer – Steve May 22 '13 at 15:52
  • Thanks, the reason is why I will let the user to edit the INI changes by adding,deleting values, so the user can write in lower or upper you know.I know how to change the case of the string but maybe is not 100% perfect that's why I ask for a ignorecase or something easy, see my update please – ElektroStudios May 22 '13 at 15:55