Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Getting REG_MULTI_SZ values into a listbox

Getting REG_MULTI_SZ values into a listbox

Scheduled Pinned Locked Moved C#
windows-admindata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucy
    wrote on last edited by
    #1

    Hi I have 2 listboxes on my form and in the form load event I want to get values from specified registry keys to appear as items in to each listbox. The first listbox works fine using this code: RegistryKey key1 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key1.GetValue("MultiString1") != null) { ListBox1.Items.Clear(); string[] items = (string[])key1.GetValue("MultiString1"); for (int i = 0; i < items.Length; i++) { ListBox1.Items.Add(key1.GetValue("MultiString1", items[i])); } } But for the second listbox I have practically the exact same code (But with a different registry value name and listbox). However, when the form loads ListBox1 shows the correct values but ListBox2 shows 'String[]Array' for each number of multistring values. Here is my code for getting the values from "MultiString2" into ListBox2: RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key2.GetValue("MultiString2") != null) { ListBox2.Items.Clear(); string[] items = (string[])key2.GetValue("MultiString2"); for (int i = 0; i < items.Length; i++) { ListBox2.Items.Add(key2.GetValue("MultiString2", items[i])); } } Why do I have this error when both sections of code are not that different? Can anyone advise me please? Lucy

    L D 2 Replies Last reply
    0
    • L Lucy

      Hi I have 2 listboxes on my form and in the form load event I want to get values from specified registry keys to appear as items in to each listbox. The first listbox works fine using this code: RegistryKey key1 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key1.GetValue("MultiString1") != null) { ListBox1.Items.Clear(); string[] items = (string[])key1.GetValue("MultiString1"); for (int i = 0; i < items.Length; i++) { ListBox1.Items.Add(key1.GetValue("MultiString1", items[i])); } } But for the second listbox I have practically the exact same code (But with a different registry value name and listbox). However, when the form loads ListBox1 shows the correct values but ListBox2 shows 'String[]Array' for each number of multistring values. Here is my code for getting the values from "MultiString2" into ListBox2: RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key2.GetValue("MultiString2") != null) { ListBox2.Items.Clear(); string[] items = (string[])key2.GetValue("MultiString2"); for (int i = 0; i < items.Length; i++) { ListBox2.Items.Add(key2.GetValue("MultiString2", items[i])); } } Why do I have this error when both sections of code are not that different? Can anyone advise me please? Lucy

      L Offline
      L Offline
      Lucy
      wrote on last edited by
      #2

      I have tried changing ListBox2.Items.Add(key2.GetValue("MultiString2", items[i])); to ListBox2.Items.Add(key2.GetValue("MultiString2", items[i].ToString)); But the same problem occurs. All registry key and value names are correct. Stepping through the code I can see that the value for ListBox1.Items is {System.Windows.Forms.ListBox.ObjectCollection} Should the listbox items be an ObjectCollection? Lucy -- modified at 8:03 Friday 28th September, 2007

      1 Reply Last reply
      0
      • L Lucy

        Hi I have 2 listboxes on my form and in the form load event I want to get values from specified registry keys to appear as items in to each listbox. The first listbox works fine using this code: RegistryKey key1 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key1.GetValue("MultiString1") != null) { ListBox1.Items.Clear(); string[] items = (string[])key1.GetValue("MultiString1"); for (int i = 0; i < items.Length; i++) { ListBox1.Items.Add(key1.GetValue("MultiString1", items[i])); } } But for the second listbox I have practically the exact same code (But with a different registry value name and listbox). However, when the form loads ListBox1 shows the correct values but ListBox2 shows 'String[]Array' for each number of multistring values. Here is my code for getting the values from "MultiString2" into ListBox2: RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\RegKey_test"); if (key2.GetValue("MultiString2") != null) { ListBox2.Items.Clear(); string[] items = (string[])key2.GetValue("MultiString2"); for (int i = 0; i < items.Length; i++) { ListBox2.Items.Add(key2.GetValue("MultiString2", items[i])); } } Why do I have this error when both sections of code are not that different? Can anyone advise me please? Lucy

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Lucy_H85 wrote:

        string[] items = (string[])key2.GetValue("MultiString2"); for (int i = 0; i < items.Length; i++) { ListBox2.Items.Add(key2.GetValue("MultiString2", items[i])); }

        You already got the list from the registry in the first line, then you go and get the list again, and again, and again, and again, and again, for each and every pass through this loop. There's no need to do that. You already have the list in a string array you chose to ignore.

        string[] items = (string[])key2.GetValue("MultiString2");
        for (int i = 0; i < items.Length; i++)
        {
        ListBox2.Items.Add(items[i]);
        }

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups