Favorites With Registry

Azreal

Member
Joined
Oct 4, 2006
Messages
19
Programming Experience
Beginner
Hello, I am currently trying to make a web browser with Visual Studio 2005 and I am stuck with the favorites. Instead of using data files for the option I am using the registry. I was showed how to loop throgh a certain regisrty key and get all of the strings from it. Here is the code for that:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] regKey [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Microsoft.Win32.RegistryKey = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.Registry.CurrentUser.OpenSubKey([/SIZE][SIZE=2][COLOR=#800000]"Software\Web Browser\Favorites\"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] valueNames [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]() = regKey.GetValueNames()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] upperBound [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = valueNames.GetUpperBound(0)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] values(upperBound) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] upperBound [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] 1[/SIZE]
[SIZE=2][SIZE=2]values(i) = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](regKey.GetValue(valueNames(i)))[/SIZE]
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i[/SIZE]
[SIZE=2]cboFavorites.Items.AddRange(values)[/SIZE]

This adds the favorites from the registry into a combo box from the Data of the string.

I made it so it read the String's Name instead by changing
"values(i) = CStr(regKey.GetValue(valueNames(i)))" to
"values(i) = CStr(valueNames(i))"

I was wondering if when you click the combo box on a string, ie: google (with "http://www.google.com/" as the data) it would go to google's site.
I tried several different ways and couldn't get it.

You would see String Name, Web Browser would go to Data.

___|String Name|___________________|Data|__________________|
___|Google______|___________________|http://www.google.com/__|
___|MSN________|___________________|http://www.msn.com/____|
___|Yahoo_______|___________________|http://www.yahoo.com/__|

How would I do this?

Thanks​
 
This would be the way to display the friendly name while exposing the full URL:
VB.NET:
Imports Microsoft.Win32

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim regKey As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("key path here")
        Dim valueNames As String() = regKey.GetValueNames() 'Get all the friendly names.
        Dim upperBound As Integer = valueNames.GetUpperBound(0)
        Dim favourites As New List(Of Favourite)

        'Get all the full addresses and connect them to the corresponding friendly name.
        For Each name As String In valueNames
            favourites.Add(New Favourite(name, CStr(regKey.GetValue(name))))
        Next name

        Me.ComboBox1.DisplayMember = "Name" 'Display the friendly names in the control.
        Me.ComboBox1.ValueMember = "Url" 'Expose the full address via the SelectedValue.
        Me.ComboBox1.DataSource = favourites
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Get the full address that corresponds to the selected name.
        MessageBox.Show(String.Format("The address for '{0}' is '{1}'.", _
                                      Me.ComboBox1.GetItemText(Me.ComboBox1.SelectedItem), _
                                      Me.ComboBox1.SelectedValue))
    End Sub
End Class

Public Structure Favourite

    Private _name As String
    Private _url As String

    'The friendly name.
    Public Property Name() As String
        Get
            Return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
        End Set
    End Property

    'The full address.
    Public Property Url() As String
        Get
            Return Me._url
        End Get
        Set(ByVal value As String)
            Me._url = value
        End Set
    End Property

    Public Sub New(ByVal name As String, ByVal url As String)
        Me._name = name
        Me._url = url
    End Sub

End Structure
You can use that code to see how it works. All you need to change is the registry path and the name of the ComboBox.
 
I am having trouble with this code when I use it on another combobox. It loads the valuemember fine, but it doesn't show the right text. It just shows: "Pluto_Web_Browser.Favourite" so it shows The project name as a string and .Favourite, the Structure. How can I fix this? All I did was copy it and change the combobox name and registry value.
 
You haven't set the DisplayMember properly so it's just calling ToString on the objects you've bound, which just returns the type of the object. That's why you're seeing "Pluto_Web_Browser.Favourite": because that's the type of the bound objects. Chances are that you haven't changed the name of the ComboBox for which you're setting the DisplayMember property.
 
I don't understand, I thought I did it right, here is the code:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] regKey [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] RegistryKey = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.Registry.CurrentUser.OpenSubKey([/SIZE][SIZE=2][COLOR=#800000]"Software\JFenske\Pluto\URLS\"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] valueNames [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]() = regKey.GetValueNames() [/SIZE][SIZE=2][COLOR=#008000]'Get all the friendly names.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] upperBound [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = valueNames.GetUpperBound(0)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] favourites [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of[/COLOR][/SIZE][SIZE=2] Favourite)[/SIZE]
[SIZE=2][COLOR=#008000]'Get all the full addresses and connect them to the corresponding friendly name.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] valueNames[/SIZE]
[SIZE=2]favourites.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Favourite(name, [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](regKey.GetValue(name))))[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] name[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtURL.DisplayMember = [/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Display the friendly names in the control.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtURL.ValueMember = [/SIZE][SIZE=2][COLOR=#800000]"Url"[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Expose the full address via the SelectedValue.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtURL.DataSource = favourites[/SIZE]
 
What does definition of the Favourite structure look like? If I change the name of the "Name" property to something else then I get the same result you're talking about, which is exactly what I would expect. If the DisplayMember doesn't match the name of a property then it's just ignored, so I'd guess that your Favourite type doesn't have a property called "Name".
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Structure[/COLOR][/SIZE][SIZE=2] Favourite
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _url [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'The friendly name.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Name() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._name
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._name = value
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'The full address.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Url() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._url
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._url = value
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] url [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._name = name
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._url = url
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Structure
[/COLOR][/SIZE]

That is my Favourite Structure
 
I still don't understand what is wrong, I have looked at it all night and now a little this morning, I've changed stuff and altered it and it still won't work correctly. I don't understand.
 
From what you've posted I can't see an issue either. The issue you describe indicates that you haven't set the DisplayMember properly but from the code it looks like you have. Can't help you more I'm afraid.
 
You are both talking about Combobox, but in code posted by Azreal it said bound to txtURL which indicated a Textbox.. could there be a mixup of handled controls here?
 
You are both talking about Combobox, but in code posted by Azreal it said bound to txtURL which indicated a Textbox.. could there be a mixup of handled controls here?
No it is a combobx. I had a textbox at first and changed after I already wrote the code for it.

I will look at it now that I am home and if I don't get it I will try something new.

Thanks for your help :)
 
Back
Top