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. Setting listbox items to the registry

Setting listbox items to the registry

Scheduled Pinned Locked Moved C#
windows-adminhelpquestion
8 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

    I have a listbox on a form that displays data from a registry key of value multistring. This listbox is filled when the form loads by this code: RegistryKey getItems = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegKey_test"); if (getItems.GetValue("Items") != null) { string[] itemName = (string[])getItems.GetValue("Items"); for (int i = 0; i < itemName.Length; i++) { listBox1.Items.Add(getItems.GetValue("Items", itemName)); } } This works fine. But when I want to set the listbox items to the registry with the following code, an error occurs: if (getItems.GetValue("Items") != null) { Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Items", listBox1.Items.ToString(), RegistryValueKind.MultiString); } I no that something is wrong with this line of code but I am not sure what it is. Can anyone advise me where I am going wrong please? Lucy

    G 1 Reply Last reply
    0
    • L Lucy

      I have a listbox on a form that displays data from a registry key of value multistring. This listbox is filled when the form loads by this code: RegistryKey getItems = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegKey_test"); if (getItems.GetValue("Items") != null) { string[] itemName = (string[])getItems.GetValue("Items"); for (int i = 0; i < itemName.Length; i++) { listBox1.Items.Add(getItems.GetValue("Items", itemName)); } } This works fine. But when I want to set the listbox items to the registry with the following code, an error occurs: if (getItems.GetValue("Items") != null) { Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Items", listBox1.Items.ToString(), RegistryValueKind.MultiString); } I no that something is wrong with this line of code but I am not sure what it is. Can anyone advise me where I am going wrong please? Lucy

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      What error are you getting? Is it an exception? What I don't like is

      listBox1.Items.ToString()

      as listBox1.Items returns ObjectCollection and when you convert it to string you will get name of the type and not the items of listbox.

      #region signature my articles #endregion

      L 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        What error are you getting? Is it an exception? What I don't like is

        listBox1.Items.ToString()

        as listBox1.Items returns ObjectCollection and when you convert it to string you will get name of the type and not the items of listbox.

        #region signature my articles #endregion

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

        I didnt think that part was right. The error it returns is: The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted. If I just leave it as listBox1.Items the same error occurs.

        G 1 Reply Last reply
        0
        • L Lucy

          I didnt think that part was right. The error it returns is: The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted. If I just leave it as listBox1.Items the same error occurs.

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          I guess you are getting ArgumentException aren't you? According to MSDN: "If the type of the specified value does not match the specified valueKind, and the data cannot be converted, ArgumentException is thrown" If you have a look at definition of RegistryValueKind.MultiString it says: "Specifies an array of null-terminated strings, terminated by two null characters. This value is equivalent to the Win32 API registry data type REG_MULTI_SZ." In reality listbox.Items.ToString() retuns just a string while listbox.Items retuns ObjectCollection which is incompatible too. That's why you are getting the exception. What are you trying to achieve?

          #region signature my articles #endregion

          L 1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            I guess you are getting ArgumentException aren't you? According to MSDN: "If the type of the specified value does not match the specified valueKind, and the data cannot be converted, ArgumentException is thrown" If you have a look at definition of RegistryValueKind.MultiString it says: "Specifies an array of null-terminated strings, terminated by two null characters. This value is equivalent to the Win32 API registry data type REG_MULTI_SZ." In reality listbox.Items.ToString() retuns just a string while listbox.Items retuns ObjectCollection which is incompatible too. That's why you are getting the exception. What are you trying to achieve?

            #region signature my articles #endregion

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

            Yes I am getting an ArgumentException. I have a form with a listbox. When the form loads, each string in the multistring registry key is put into the listbox as an item. The user can then edit these items as well as removing and adding items. The items then need to be set as values back in the multistring registry key by the click of a button. Does that make sense?

            G 1 Reply Last reply
            0
            • L Lucy

              Yes I am getting an ArgumentException. I have a form with a listbox. When the form loads, each string in the multistring registry key is put into the listbox as an item. The user can then edit these items as well as removing and adding items. The items then need to be set as values back in the multistring registry key by the click of a button. Does that make sense?

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              Lucy_H85 wrote:

              Does that make sense?

              Yes it does. Don't be rude. As I have already written RegistryValueKind.MultiString needs an array of strings. What you are passing is collection of objects. In my opinion you should create an array of string which will contain items from listbox and pass it to SetValue. Hope it helps

              #region signature my articles #endregion

              L 1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                Lucy_H85 wrote:

                Does that make sense?

                Yes it does. Don't be rude. As I have already written RegistryValueKind.MultiString needs an array of strings. What you are passing is collection of objects. In my opinion you should create an array of string which will contain items from listbox and pass it to SetValue. Hope it helps

                #region signature my articles #endregion

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

                Appologies if I sounded rude but I was not being rude at all. I am not very good at explaining things sometimes so I was genuinly asking if my description was confusing or not. Thanks for your comments I will try it out now. Lucy

                G 1 Reply Last reply
                0
                • L Lucy

                  Appologies if I sounded rude but I was not being rude at all. I am not very good at explaining things sometimes so I was genuinly asking if my description was confusing or not. Thanks for your comments I will try it out now. Lucy

                  G Offline
                  G Offline
                  Giorgi Dalakishvili
                  wrote on last edited by
                  #8

                  Lucy_H85 wrote:

                  Appologies if I sounded rude but I was not being rude at all.

                  It's ok :) Please right back if you are still experiencing problems.

                  #region signature my articles #endregion

                  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