1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Fonts and colors with the registry

Discussion in 'C#' started by x64, May 15, 2007.

  1. x64

    x64 New Member

    Joined:
    Apr 16, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    St.Paul Minnesota, USA
    Hello again, I am nearing completion on my current project but I am stumped on reading and writing to the registry. I am using Francesco Natali's ModifyRegistry.cs from Code Project to help me do this. I have been successful in reading and writing int and string values but I am stuck on Fonts and Colors.

    My main question is: What would I convert Fonts and Colors to, to write them into the registry? I have converted a font into a string to get it to write into the registry but I cannot read it as a string because "cannot convert from 'string' to 'System.Drawing.Font'" but I can convert a font to a string?

    Thanks in advance for any help.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Construct the font object with the font name as one of the parameter in constructor.
     
  3. x64

    x64 New Member

    Joined:
    Apr 16, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    St.Paul Minnesota, USA
    That doesn't make since. If it gets written as a string to the registry, why can't I read it as a string? Or at least convert the string back to a Font. I can't add the name of the font in there, it will be different every time the user picks a different one. I am wanting to make it so what ever the user picks remains the default on start up.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Basically if its written as string then its read as string. Now the string to font conversion is done through the constructor. You are not writing the Font object in registry or I am not sure if its possible as well.
     
  5. x64

    x64 New Member

    Joined:
    Apr 16, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    St.Paul Minnesota, USA
    Ok well, I ran my program and I selected a simple font for it, here is what was put into the registry. [Font: Name=Wingdings 2, Size=8.25, Units=3, GdiCharSet=2, GdiVerticalFont=False] I used
    Code:
    [size=2][color=#0000ff]private [/color][/size][size=2][color=#2b91af]Font[/color][/size][size=2] preffont;[/size]
     
    [size=2][size=2][/size][size=2][color=#0000ff]public [/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] WriteFormattingPrefsToRegistry()[/size]
    [size=2]{
    [/size][/size]reg.Write("font", GetFont()); 
    }
     
    [size=2][color=#0000ff]public [/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] ReadFormattingPrefsFromRegistry()[/size]
    [size=2]{
    reg.BaseRegistryKey = [/size][size=2][color=#2b91af]Registry[/color][/size][size=2].CurrentUser;[/size]
    [color=#008000]//This does not work with or without converting to a string.[/color]
    [size=2]SetFont([/size][size=2][color=#2b91af]Convert[/color][/size][size=2].ToString(reg.Read([/size][size=2][color=#a31515]"font"[/color][/size][size=2]))); [/size]
    [size=2][size=2][/size][/size][size=2]}[/size]
     
    [size=2]
    [/size][size=2][color=#0000ff]
    public [/color][/size][size=2][color=#2b91af]Font[/color][/size][size=2] GetFont()
    {
    [/size][size=2][color=#0000ff]return[/color][/size][size=2] preffont;
    }
     
    [/size][size=2][color=#0000ff]public[/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] SetFont([/size][size=2][color=#2b91af]Font[/color][/size][size=2] v)
    {
    preffont = v;
    }[/size]
    to write it to the registry, using a class with all the reg.write code. The Font object its self writes to the registry as a string automatically.
    Thats what I was thinking as well, but I guess not.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    When you have written the font into the registry there will be an automatically string conversion but the vice versa is not true. Something like you always have a tostring but the reverse is not true for any object
     
  7. x64

    x64 New Member

    Joined:
    Apr 16, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    St.Paul Minnesota, USA
    Alright thanks, I'll be sure to post a solution if I find out how to solve this problem.
     

Share This Page