Results 1 to 7 of 7

Thread: [RESOLVED] How to programatically make a label font bold

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    283

    Resolved [RESOLVED] How to programatically make a label font bold

    Can anyone show me how to programatically make a label font bold?

    lblid.Font.Bold = True

    I tried the above, but it did not work and gave Read Only Error.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to programatically make a label font bold

    Check just type outside of the IDE

    lblid.Font = New Font("Arial',10,Font.Bold")
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Barrie Ontario
    Posts
    54

    Re: How to programatically make a label font bold

    I don't know if this will help you but I had a similar problem. try setting the Generatemember of the label to true. I found that this allowed me to reference the label in code.

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: How to programatically make a label font bold

    The GenerateMember property will be set to true by default, that is not the problem.

    The problem is that Font objects are immutable: you cannot change them after you created them, because all their properties (Size, Bold, etc) are read-only.

    To change the font, you can create a new Font and use one of the many overloads of the constructor. GaryMazzone showed you one:
    Code:
    lblId.Font = New Font("Arial", 10, FontStyle.Bold)
    However, this will create the font as Arial, regardless of what it was before.

    If you want to keep the font the same 'family' (arial, times new roman, etc) and the same size, but merely want to change the style, there is a better suited constructor overload. This one accepts another Font object and a FontStyle. It will then create a new font that has all properties the same as the Font you pass, except for the style, which it will set to whatever you specify as the second argument.

    So, if you want to make a font bold, you can do this
    Code:
    lblId.Font = New Font(lblId.Font, FontStyle.Bold)
    Furthermore, you can use bitwise combinations for the FontStyle. For example, to set a font as Bold and Italic, you use
    Code:
    lblId.Font = New Font(lblId.Font, FontStyle.Bold Or FontStyle.Italic)
    If you want your font to be italic, but also keep all other styles (if it was bold, it has to stay bold), you simply 'Or' the Italic style to the existing style:
    Code:
    lblId.Font = New Font(lblId.Font, lblId.Font.FontStyle Or FontStyle.Italic)
    Similarly, if you want to ensure that your font is NOT bold, you can use the And Not (bitwise) operator to 'remove' the Bold style from the existing style
    Code:
    lblId.Font = New Font(lblId.Font, lblId.Font.FontStyle And Not FontStyle.Bold)
    Finally, you can use the Xor operator to toggle a style. Let's say your font was underlined, it will then become not underlined (and vice versa)
    Code:
    lblId.Font = New Font(lblId.Font, lblId.Font.FontStyle Xor FontStyles.Underline)
    These bitwise operations (Or, And, Not, Xor, etc) work because FontStyle is an enumeration. Basically, the Or operator 'adds' two styles together. The 'And Not' operator 'removes' one style. The Xor operator 'toggles' one style.

  5. #5
    Member
    Join Date
    Feb 2010
    Location
    Barrie Ontario
    Posts
    54

    Re: How to programatically make a label font bold

    Very well posted Nick, it was not my problem but it helped me avoid one. Sorry for the generate member thing it was just a mistake I have learned from was guessing it may apply here as well.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to programatically make a label font bold

    Quote Originally Posted by NickThissen View Post
    These bitwise operations (Or, And, Not, Xor, etc) work because FontStyle is an enumeration. Basically, the Or operator 'adds' two styles together. The 'And Not' operator 'removes' one style. The Xor operator 'toggles' one style.
    Well said. I'll just make one comment. It's not just the fact that it's an enumeration that allows the use of bitwise operations. An enumeration with sequential values, e.g. 1, 2, 3, 4, 5, 6, would support bitwise operations but they wouldn't do anything useful. The values must be powers of 2, e.g. 1, 2, 4, 8, 16, 32, for the bitwise operations to be useful. That's because, in binary form, those numbers each have 1 and one 1 bit set:
    Code:
      1 = 00000001
      2 = 00000010
      4 = 00000100
      8 = 00001000
     16 = 00010000
     32 = 00100000
     64 = 01000000
    128 = 10000000
    A bitwise OR, for instance, will create a result where a bit is set in the result if that bit is set in one operand OR the other, e.g.
    Code:
    32 00100000
     4 00000100
    ----------- OR
    36 00100100
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width