Results 1 to 6 of 6

Thread: Populate ComboBox via Configuration file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    96

    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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    96

    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.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,385

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Lively Member
    Join Date
    Jul 2022
    Posts
    73

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,687

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    96

    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
  •  



Click Here to Expand Forum to Full Width