Connecting Tech Pros Worldwide Forums | Help | Site Map

Saving background color VB 2008

Newbie
 
Join Date: Jan 2009
Posts: 13
#1: May 17 '09
I am trying to save the background color of my main textbox so that if a user changes it, those changes will be automatically save and retreived when the user re-opens the application. The code builds fine, but throws and error, saying "specified cast is not valid". It appears that the registry only accepts string values, but attempting to convert to a string has the problem of getting it back to a color value. So far nothing I have tried has worked. . . . This is VB 2008 . . . .
Expand|Select|Wrap|Line Numbers
  1. Dim bColor As Color
  2. Try
  3. bColor = Me.RichTextBox1.BackColor
  4.  
  5. My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\ASM\CanvasColor", "bColor", bColor)
  6.  
  7. bColor = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\ASM\CanvasColor", "bColor", Color.LightGray)
  8.  
  9. Me.RichTextBox1.BackColor = bColor
  10. Catch ex As Exception
  11. MsgBox(ex.Message & "  $%#&*!!!!")
  12. End Try
  13.  

PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#2: May 18 '09

re: Saving background color VB 2008


Assuming that you have a valid string color in your string
Expand|Select|Wrap|Line Numbers
  1. string color = "blue";
  2.  
that you read from registry or maybe xml file or database you can use
Expand|Select|Wrap|Line Numbers
  1. button1.BackColor = System.Drawing.Color.FromName(color.Trim());
  2.  
You cant assign a string as background.. You need Color type...
Newbie
 
Join Date: Jan 2009
Posts: 13
#3: May 18 '09

re: Saving background color VB 2008


This is Visual Basic 2008, not C++, no "Trim" function is available for
color . . . .

The following is the solution to the problem . . .

Expand|Select|Wrap|Line Numbers
  1.             'Save color to registry
  2.             Dim bColor As Color = Me.RichTextBox1.BackColor
  3.             My.Computer.Registry.SetValue(regPath, "bColor", bColor.ToArgb())
  4.  
  5.  
  6.             ' Retrieve color from registry
  7.             bColor = Color.FromArgb(My.Computer.Registry.GetValue_
  8.                               (regPath, "bColor", Color.LightGray))
  9.             Me.RichTextBox1.BackColor = bColor
Reply

Tags
saving background color