Combobox aus App.Config Füllen

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von r0tzi.

    Combobox aus App.Config Füllen

    Hallo Zusammen,

    zuerst möchte ich mal ein riesen kompliment an das Forum hier richten, hat mir bisher immer super geholfen. Nun hab ich auch mal eine Frage, wo ich einfach nicht weiter komme.

    Es geht um die App Config aka "Project.Exe.config" die im Programm verzeichniss abliegt.

    Ich möchte meine Combox mit Werten Füllen die in dieser config eingetragen sind. Diese sieht z.B. so aus

    <add key="comboName1" value="Name" />
    <add key="Link1G:\" value="G:\" />
    <add key="comboName2" value="Name" />
    <add key="Link2G:\" value="G:\" />
    <add key="comboName3" value="Name" />
    <add key="Link3G:\" value="G:\" />

    Nun möchte ich beim Programm start alle keys mit namensinhalt "combo" in die Combobox laden. Also hier wären es 3.

    mit:
    Me.ComboBox1.Items.Add(System.Configuration.ConfigurationManager.AppSettings("comboname1"))
    funktioniert es.

    ich habe schon ("combo*") , ("combo*?") oder ("combo?") mit "platzhaltern" probiert... geht alles nicht, da er wohl den genauen Text abfrägt.

    Hat jemand eine Idee oder eine Lösung?

    Vielen Dank
    Hallo @r0tzi
    Du kannst eine Schlaufe über die AppSettings machen und die einzelnen Keys nach "combo" abfragen.

    VB.NET-Quellcode

    1. Dim appSettings As NameValueCollection = ConfigurationManager.AppSettings
    2. For Each key As String In appSettings.AllKeys
    3. If key.Contains("combo") Then
    4. ComboBox1.Items.Add(appSettings(key))
    5. End If
    6. Next

    Gruss HenryV

    HenryV schrieb:

    Du kannst eine Schlaufe über die AppSettings machen
    ...oder das ganze unter Verwendung von LINQ machen:

    VB.NET-Quellcode

    1. Dim appSettings = Configuration.ConfigurationManager.AppSettings
    2. Dim comboAppSettings = appSettings.AllKeys.ToDictionary(Of String, String)(
    3. Function(k)
    4. Return k
    5. End Function,
    6. Function(k)
    7. Return appSettings(k)
    8. End Function
    9. ).Where(Function(x)
    10. Return x.Key.Contains("combo")
    11. End Function
    12. ).Select(Function(x)
    13. Return x.Value
    14. End Function
    15. ).Cast(Of Object)().ToArray()
    16. Me.ComboBox1.Items.AddRange(comboAppSettings)

    Sieht zwar minimal komplizierter aus, hat aber den Vorteil, dass du hier bessere Kontrolle über die Datentypen hast.
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell