listbox

G

Guest

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
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?Qm9iYnkgSg==?= said:
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)'.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?Ym9iYnkgag==?= said:
"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
///
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?Ym9iYnkgag==?= said:
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("My App")
Dim name As String = CType(key.GetValue("My App"), String)

HoldColors = name
 
G

Guest

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("My 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 !!!
 

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