By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,778 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,778 IT Pros & Developers. It's quick & easy.

Saving background color VB 2008

P: 14
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.  
May 17 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 750
PRR
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...
May 18 '09 #2

P: 14
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
May 18 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.