Saving background color VB 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • desertavataraz
    New Member
    • Jan 2009
    • 14

    Saving background color VB 2008

    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 . . . .
    Code:
    Dim bColor As Color
    Try
    bColor = Me.RichTextBox1.BackColor
    
    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\ASM\CanvasColor", "bColor", bColor)
    
    bColor = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\ASM\CanvasColor", "bColor", Color.LightGray)
    
    Me.RichTextBox1.BackColor = bColor
    Catch ex As Exception
    MsgBox(ex.Message & "  $%#&*!!!!")
    End Try
    Last edited by debasisdas; May 18 '09, 09:08 AM. Reason: thread moved to .Net section of the forum
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Assuming that you have a valid string color in your string
    Code:
    string color = "blue";
    that you read from registry or maybe xml file or database you can use
    Code:
    button1.BackColor = System.Drawing.Color.FromName(color.Trim());
    You cant assign a string as background.. You need Color type...

    Comment

    • desertavataraz
      New Member
      • Jan 2009
      • 14

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

      The following is the solution to the problem . . .

      Code:
                  'Save color to registry
                  Dim bColor As Color = Me.RichTextBox1.BackColor
                  My.Computer.Registry.SetValue(regPath, "bColor", bColor.ToArgb())
                  
      
                  ' Retrieve color from registry
                  bColor = Color.FromArgb(My.Computer.Registry.GetValue_
                                    (regPath, "bColor", Color.LightGray))
                  Me.RichTextBox1.BackColor = bColor
      Last edited by tlhintoq; May 19 '09, 03:10 PM. Reason: [CODE] ... your code here ... [/CODE] tags added

      Comment

      Working...