Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Convert Font size

Convert Font size

Scheduled Pinned Locked Moved C#
windows-adminquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucy
    wrote on last edited by
    #1

    HI I have created an application that uses the registry. On the application there are two textboxes and a button. When the form is loaded the first textbox displays a font that has been declared in the registry and the second text box displays the font size, again declared in the registry. The button allows the user to change the font and font size using a FontDialog. When this dialog is opened (by clicking the button), the selected font is the font declared in the registry. I also want the font size that is declared in the registry to be selected in the dialog. The font name works perfectly fine, I am just having trouble with converting the font size. First of here is my code: string font; int size; FontDialog selectFont = new FontDialog(); font = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", "")); selectFont.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(font); size = Convert.ToInt32(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", "")); selectFont.Font.Size = (Size)TypeDescriptor.GetConverter(typeof(Size)).ConvertFrom(size); if (selectFont.ShowDialog() == DialogResult.OK) { Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", selectFont.Font.Name.ToString()); TBFont.Text = selectFont.Font.Name.ToString(); Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", selectFont.Font.Size, RegistryValueKind.DWord); TBFontSize.Text = selectFont.Font.Size.ToString(); } I am unsure if I should set the variable size as an integer or a string, and then according to this how is it converted to Font.Size? Lucy

    M 1 Reply Last reply
    0
    • L Lucy

      HI I have created an application that uses the registry. On the application there are two textboxes and a button. When the form is loaded the first textbox displays a font that has been declared in the registry and the second text box displays the font size, again declared in the registry. The button allows the user to change the font and font size using a FontDialog. When this dialog is opened (by clicking the button), the selected font is the font declared in the registry. I also want the font size that is declared in the registry to be selected in the dialog. The font name works perfectly fine, I am just having trouble with converting the font size. First of here is my code: string font; int size; FontDialog selectFont = new FontDialog(); font = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", "")); selectFont.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(font); size = Convert.ToInt32(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", "")); selectFont.Font.Size = (Size)TypeDescriptor.GetConverter(typeof(Size)).ConvertFrom(size); if (selectFont.ShowDialog() == DialogResult.OK) { Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", selectFont.Font.Name.ToString()); TBFont.Text = selectFont.Font.Name.ToString(); Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", selectFont.Font.Size, RegistryValueKind.DWord); TBFontSize.Text = selectFont.Font.Size.ToString(); } I am unsure if I should set the variable size as an integer or a string, and then according to this how is it converted to Font.Size? Lucy

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      Lucy_H85 wrote:

      selectFont.Font.Size = (Size)TypeDescriptor.GetConverter(typeof(Size)).ConvertFrom(size);

      I think Size property is readonly. so, try like that.. Example 1 Label1.Font = New Font("Tahoma", 30) Example 2 font = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", "")); size = Convert.ToInt32(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", "")); selectFont.Font = new Font(font,size);

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

      L 1 Reply Last reply
      0
      • M Michael Sync

        Lucy_H85 wrote:

        selectFont.Font.Size = (Size)TypeDescriptor.GetConverter(typeof(Size)).ConvertFrom(size);

        I think Size property is readonly. so, try like that.. Example 1 Label1.Font = New Font("Tahoma", 30) Example 2 font = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "Font", "")); size = Convert.ToInt32(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RegKey_test", "FontSize", "")); selectFont.Font = new Font(font,size);

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

        L Offline
        L Offline
        Lucy
        wrote on last edited by
        #3

        That works great! Thanks! Didnt realise it would be so easy!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups