-
Sep 25th, 2023, 07:58 AM
#1
Thread Starter
Lively Member
Populate ComboBox via Configuration file
Hello there,
I have one problem and no idea how to resolve it.
App.config file has some value
Code:
<add key="MyCU" value="ee, rr, oo" />
from which I want each string part to be a new line in a ComboBox > List -- ee | rr | oo
I have code working if I want to get value as it is
Code:
ReadOnly CU As String = MYConfigReader.GetValue("MyCU", GetType(String))
...
ComboBox.Text = CU
I want ComboBox.Item.Add(CUitems) in a way that ComboBox gets values ee|rr|oo and all other values written inside App.config file
I have tried many ways but without result. I appreciate any help. Thank you all
-
Sep 25th, 2023, 08:06 AM
#2
Thread Starter
Lively Member
Re: Populate ComboBox via Configuration file
Code:
MessageBox.Show(CU.ToString)
Dim NewCU As String = CU
Dim MyList = NewCU.Split(","c).ToString
MessageBox.Show(MyList.ToString)
ComboBox.Items.Add(MyList)
Code above is my checkpoint, and an idea how it should work. First message box shows me the correct string, second message box shows me System.String[] and finally Combo box gets System.String[] value as well.
-
Sep 25th, 2023, 09:59 AM
#3
Re: Populate ComboBox via Configuration file
Right because the defaulot implementation of ToString on an array is return the type of array it is. When you call .Add to add an object, by default it calls .ToString on that object... which I already explained.
Fortunately there should be an .AddRange method that accepts an array that you can use to add all items in the array.
-tg
-
Sep 25th, 2023, 12:19 PM
#4
Lively Member
Re: Populate ComboBox via Configuration file
As techgome pointed out you can use .AddRange, here is a stack overflow post showing a couple of ways to take an array and fill a combobox https://stackoverflow.com/questions/...a-string-array
-
Sep 25th, 2023, 12:24 PM
#5
Re: Populate ComboBox via Configuration file
Guessing,
Code:
Dim settings As System.Collections.Specialized.NameValueCollection = System.Configuration.ConfigurationManager.AppSettings
For Each key As String In settings.AllKeys
' based on <add key="MyCU" value="ee, rr, oo" />
If key = "MyCU" Then
Dim val As String = System.Configuration.ConfigurationManager.AppSettings(key)
Dim vals() As String = val.Split(New Char() {","c, " "c}, StringSplitOptions.RemoveEmptyEntries)
ComboBox1.Items.AddRange(vals)
End If
Next
-
Sep 25th, 2023, 03:47 PM
#6
Thread Starter
Lively Member
Re: Populate ComboBox via Configuration file
Thanks, it works easily as copy/paste into my code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|