Registry enigma - please help!

G

Guest

Getting a string of boolean value into and out of the registry is no problem.
Here's the problem:

Although you can place an object into the registry and retreive it, I need
to place an ArrayList object with 10 string items into the registry and
retreive them later. I tried this:

key.SetValue("lstNSXitems", lstNSX.Items)

where "lstNSXitems" is the name of the subkey, and lstNSX.Items is the
ArrayList with the 10 items. To retreive the ArrayList, I used this:

If key.GetValue("lstNSXitems") Is Nothing Then
Me.lstNS.Items.AddRange(New Object() {"A", "B", "C", "D", _
"E", "F", "G", "H", _
"I", "J"})
Else
Me.lstNSX.Items.AddRange(New Object()
{key.GetValue("lstNSXitems")})
End If

All this does is place "System.Windows.Forms.ListBox+ObjectCollection" into
the ArrayList on the form.

Can anyone tell me where my logic went south? I don't know if I messed up
the beginning, the end or both.

Thanks for any help. I'm really stumped on this one.
 
R

rowe_newsgroups

Although you can place an object into the registry and retreive it

Actually, it seems to just store Object.ToString() into the registry,
not the object itself.

Maybe you could just loop through your array and build a string like
"A,B,C,D,E,F,G,H,I,J" and then save that into the registry? Then just
use something like the following to put it back into an array:

Dim MyString as String = key.GetValue("lstNSXitems")
Dim MyArray() as String = MyString.Split(",")

Thanks,

Seth Rowe
 
G

Guest

Thanks Seth,

I'm new to VB2005 and came from VBA, so my depth of knowledge is not that
deep. The object is an ArrayList. I chose that to facilitate using a listbox
for drag and drop on the form. I thought about using a loop along with the
CreateSubTree method to create a registry entry for each item in the list. To
get the items back, I thought I'd use the SubKeyCount and GetValue inside
another loop.

It seems like a lot of extra code if the SetValue registry takes an object.
But, I am relatively new to programming in VB2005. Do you think the above
methods are okay, or is there a simpler way?

Thanks for all your help! I greatly appreciate it.
 
R

rowe_newsgroups

The object is an ArrayList. I chose that to facilitate using a listbox
for drag and drop on the form. I thought about using a loop along with the
CreateSubTree method to create a registry entry for each item in the list. To
get the items back, I thought I'd use the SubKeyCount and GetValue inside
another loop.

Um, not sure if I understand you completely. Basically, you want to use
the registry to store values that will be used to populate a listbox
right?
I thought about using a loop along with the CreateSubTree method to create a
registry entry for each item in the list.

What's the "CreateSubTree" method? Also, what is the list that holds
the values you want to write to the registry.

Sorry for all the questions, I'm trying to figure out what exactly
you're doing before I make any suggestions.

Thanks,

Seth Rowe
 
G

Guest

Hi Seth,

I finally figured it out. Here is what I have:

One form with two ListBoxes, each of which allows drag and drop
functionality. The left side ListBox is originally populated with 10 items
from a hard-coded ArrayList. the right side ListBox is initially empty.

The problem was to have persistence of the items remaining in both boxes
when the form closed and when the application closed. I couldn't use a config
file, so I used the registry.

Here is the way data to the ListBoxes operate:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

'get registry info to apply the last form position
FormSettings.ApplySettings(Me)

If Not keyNS Is Nothing Then
Dim vc1Count As Integer = keyNS.ValueCount
Dim vc2Count As Integer = keyNSX.ValueCount
If vc1Count = 0 And vc2Count = 0 Then
Me.lstNS.Items.AddRange(New Object() {"A", "B", "C", "D", _
"E", "F",
"G", "H", _
"I", "J"})
Else
For Each valueName As String In keyNS.GetValueNames()
Me.lstNS.Items.Add(keyNS.GetValue(valueName))
Next
End If
End If
If Not keyNSX Is Nothing Then
Dim vc2Count As Integer = keyNSX.ValueCount
If vc2Count = 0 Then

Else
For Each valueName As String In keyNSX.GetValueNames()
Me.lstNSX.Items.Add(keyNSX.GetValue(valueName))
Next
End If
End If

End Sub

Private Sub NSNST_FormClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

FormSettings.SaveSettings(Me)

'first, delete the subkey and all subkeys if they exist
If Not keyNSX Is Nothing Then
Dim vc2Count As Integer = keyNSX.ValueCount
If vc2Count > 0 Then
Dim sk2Name As String = ""
For Each sk2Name In keyNSX.GetValueNames()
keyNSX.DeleteValue(sk2Name)
Next
End If
End If

' Create data for the NSNST subkey.
Dim nsxCount As Integer = 0
For nsxCount = 0 To lstNSX.Items.Count - 1
keyNSX.SetValue("NSXitem" & nsxCount.ToString,
lstNSX.Items(nsxCount).ToString)
Next
keyNSX.Close()

'first, delete the subkey and all subkeys if they exist
If Not keyNS Is Nothing Then
Dim vc1Count As Integer = keyNS.ValueCount
If vc1Count > 0 Then
Dim sk1Name As String = ""
For Each sk1Name In keyNS.GetValueNames()
keyNS.DeleteValue(sk1Name)
Next
End If
End If

' Create data for the NSNST subkey.
Dim nsCount As Integer = 0
For nsCount = 0 To lstNS.Items.Count - 1
keyNS.SetValue("NSitem" & nsCount.ToString,
lstNS.Items(nsCount).ToString)
Next
keyNS.Close()

End Sub

I was using the Microsoft.Win32.RegistryKey methods to achieve it. It took
me too long to figure it out, though. I learn much better through classroom
interaction and one on one than through a book. This medium is very helpful,
too, when someone else has already tackled the problem. I must have 6 books
on VB2005, and none of them get into the registry as much as I think they
should(IMHO). :)

Thanks for helping, Seth. I greatly appreciate it.

Best regards,

JOSII
 
R

rowe_newsgroups

I must have 6 books on VB2005, and none of them get into the registry as much as I think > they should(IMHO). :)

Popular opinion seems to be against using the registry now a days.
Personnally I like using XML files instead of both registry setting and
ini files. Not only can they be easier to use (sometimes) but it also
gets you familar with a format that you will run into in various
projects. There are quite a few great suggestions on using XML archived
in this news group, you should do some quick searches for them if
you're interested. Most are based on serializable classes (like Cor's
post on using datasets) others use the XML document object in the
System.XML namespace to read/write XML files.

Thanks,

Seth Rowe
 
G

Guest

Thanks Seth,

I do remember seeing something about "serializable" in my readings, but I
think it may have been in reference to config files. I understand the basic
logic concerning the construction of an XML file, but I'm not at all familiar
with how to construct one or how to convert, let's say, a string array into
an XML file and then read it again when needed.

If I could learn that, it would likely solve a lot of problems. I don't
like to use the registry unless necessary, because it can me messy and cause
problems. It's just not a very clean way to do things.

Thanks for all your suggestions - they are very good. :)

Best regards,

JOSII
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top