469,625 Members | 1,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

listbox

I have a listbox control and I added some colors from which the user can select a color to change the background color of a couple of labels at once
The code I have looks like this
Me.ListBox1.Items.Add("Blue"
Me.ListBox1.Items.Add("White"
Me.ListBox1.Items.Add("Red"

Dim SeeColors = "Color" + "." + ListBox1.SelectedItem.tex

Me.ListBox1.BackColor = SeeColor

Just to try it out I set the background color of the listbox to the selected color

If the user selected red, as far as I know (which obviously is not very far) at this point Me.ListBox1.BackColor is suppose to contain "Color.Red

When I run the program VB.net error message says "the object variable is not set
Any suggestions

Nov 20 '05 #1
5 1944
* "=?Utf-8?B?Qm9iYnkgSg==?=" <an*******@discussions.microsoft.com> scripsit:
I have a listbox control and I added some colors from which the user can select a color to change the background color of a couple of labels at once.
The code I have looks like this:
Me.ListBox1.Items.Add("Blue")
Me.ListBox1.Items.Add("White")
Me.ListBox1.Items.Add("Red")

Dim SeeColors = "Color" + "." + ListBox1.SelectedItem.text

Me.ListBox1.BackColor = SeeColors

Just to try it out I set the background color of the listbox to the selected color


Try: 'Me.ListBox1.BackColor = [Enum].Parse(GetType(KnownColor), ListBox1.SelectedItem)'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
* "=?Utf-8?B?Ym9iYnkgag==?=" <an*******@discussions.microsoft.com> scripsit:
"Specified cast is not valid"
Is the error message I now get.


\\\
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListBox1.DataSource = [Enum].GetNames(GetType(KnownColor))
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.ListBox1.BackColor = Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor), CStr(ListBox1.SelectedItem)), KnownColor))
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
You are Ok
Thanks a bunc
It works
Nov 20 '05 #4
* "=?Utf-8?B?Ym9iYnkgag==?=" <an*******@discussions.microsoft.com> scripsit:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
HoldColors = Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor), CStr(ListBox1.SelectedItem)), KnownColor))
Me.Refresh()
Dim Appkey As Microsoft.Win32.RegistryKey
Appkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "My App")
Appkey.SetValue("My App", HoldColors)
Pass 'HoldColors.Name' to 'SetValue' instead.
I store the knew color to the registry. But the registry key value now contains: Color [Red]

which generates an error upon restart. For Instance,

Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("M y App")
Dim name As String = CType(key.GetValue("My App"), String)

HoldColors = name


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
Help !!!
I use the below code to populate a listbox with all the colors available in vb.ne

[ Dim color As System.Drawing.Colo
For Each color In System.ComponentModel.TypeDescriptor.GetConverter( GetType(Color)).GetStandardValue
Me.ListBox1.Items.Add(color.ToKnownColor
Next
-----------------------------------------------------------------------------------------------------------------------------------------
I use the code below to set the forms background color to the one selected in the listbox and save the color in the registr

[ HoldColors = Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor), CStr(ListBox1.SelectedItem)), KnownColor)

Me.Refresh(

Dim Appkey As Microsoft.Win32.RegistryKe

Appkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "My App"

Appkey.SetValue("My App", HoldColors)

Appkey.Close()

------------------------------------------------------------------------------------------------------------------------
To get the color from the registry I use the code belo

[Dim PanelColors As Microsoft.Win32.RegistryKe
PanelColors = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("M y App"

Dim nameColor As Color = CType(key.GetValue("My App"), Color

HoldColors = nameColor
---------------------------------------------------------------------------------------------------------
The problem is when I pass HoldColors to the registry it shows up in the reg as Color [Red
If I pass HoldColors.Name to the reg. only Red shows u

Either way, when I then try to get the color from the reg. either nothing shows up or I get a vb cras

How do you get Color [Red] or Red converted to Color.Red Help !!!

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.