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
  1. Home
  2. General Programming
  3. C#
  4. Font style

Font style

Scheduled Pinned Locked Moved C#
question
10 Posts 3 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.
  • M Offline
    M Offline
    Mazdak
    wrote on last edited by
    #1

    I have a label and I want to change the font style to bold or italic programmically.Can anybody tell me the code? Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

    C 1 Reply Last reply
    0
    • M Mazdak

      I have a label and I want to change the font style to bold or italic programmically.Can anybody tell me the code? Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

      C Offline
      C Offline
      Christopher Lord
      wrote on last edited by
      #2

      Based on ASP.net work (it may not be the same) Object.Font.Bold = true; Object.Font.Italic = true; Object.Font.Name = "verdana"; etc. // Rock

      M 1 Reply Last reply
      0
      • C Christopher Lord

        Based on ASP.net work (it may not be the same) Object.Font.Bold = true; Object.Font.Italic = true; Object.Font.Name = "verdana"; etc. // Rock

        M Offline
        M Offline
        Mazdak
        wrote on last edited by
        #3

        I used this code but there is an error return for it: E:\....(106): Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only Any suggestion? Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

        C J 2 Replies Last reply
        0
        • M Mazdak

          I used this code but there is an error return for it: E:\....(106): Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only Any suggestion? Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

          C Offline
          C Offline
          Christopher Lord
          wrote on last edited by
          #4

          ok, I fired up a winforms project and I have no idea. The property is listed as get only in code, but is settable in the designer. // Rock

          1 Reply Last reply
          0
          • M Mazdak

            I used this code but there is an error return for it: E:\....(106): Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only Any suggestion? Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            Most of the properties on a Font are read-only; you have to create a new font object and assign it to one you wanted to change. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

            M 1 Reply Last reply
            0
            • J James T Johnson

              Most of the properties on a Font are read-only; you have to create a new font object and assign it to one you wanted to change. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

              M Offline
              M Offline
              Mazdak
              wrote on last edited by
              #6

              Sorry James,Could you give an example?:-O Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

              J 1 Reply Last reply
              0
              • M Mazdak

                Sorry James,Could you give an example?:-O Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                Mazdak wrote: Sorry James,Could you give an example? Certainly :)

                Font newFont = new Font(myLabel.Font, // base it off the old font
                FontStyles.Bold | FontStyles.Italic // Make it bold and italic
                );
                myLabel.Font = newFont;

                If you want to change more than the font style you'll have to extract all of the needed properties from your old font and pass them to the constructor to the Font class. ms-help://MS.NETFrameworkSDK/cpref/html/frlrfsystemdrawingfontclassctortopic.htm The url above is for the .NET documentation; putting you right at the constructor list; since there's way too many for me to want to type :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                M 2 Replies Last reply
                0
                • J James T Johnson

                  Mazdak wrote: Sorry James,Could you give an example? Certainly :)

                  Font newFont = new Font(myLabel.Font, // base it off the old font
                  FontStyles.Bold | FontStyles.Italic // Make it bold and italic
                  );
                  myLabel.Font = newFont;

                  If you want to change more than the font style you'll have to extract all of the needed properties from your old font and pass them to the constructor to the Font class. ms-help://MS.NETFrameworkSDK/cpref/html/frlrfsystemdrawingfontclassctortopic.htm The url above is for the .NET documentation; putting you right at the constructor list; since there's way too many for me to want to type :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                  M Offline
                  M Offline
                  Mazdak
                  wrote on last edited by
                  #8

                  Thanks,I'll check it:) Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

                  1 Reply Last reply
                  0
                  • J James T Johnson

                    Mazdak wrote: Sorry James,Could you give an example? Certainly :)

                    Font newFont = new Font(myLabel.Font, // base it off the old font
                    FontStyles.Bold | FontStyles.Italic // Make it bold and italic
                    );
                    myLabel.Font = newFont;

                    If you want to change more than the font style you'll have to extract all of the needed properties from your old font and pass them to the constructor to the Font class. ms-help://MS.NETFrameworkSDK/cpref/html/frlrfsystemdrawingfontclassctortopic.htm The url above is for the .NET documentation; putting you right at the constructor list; since there's way too many for me to want to type :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                    M Offline
                    M Offline
                    Mazdak
                    wrote on last edited by
                    #9

                    There is a little mistake here James,you should use singular 'FontStyle';) Thanks again:) Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

                    J 1 Reply Last reply
                    0
                    • M Mazdak

                      There is a little mistake here James,you should use singular 'FontStyle';) Thanks again:) Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

                      J Offline
                      J Offline
                      James T Johnson
                      wrote on last edited by
                      #10

                      Mazdak wrote: you should use singular 'FontStyle' Oh man, today isn't my day! Earlier this morning I made another mistake in the data class thread. Maybe thats a sign that I should go to sleep! :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                      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