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.
Printable View
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.
Check just type outside of the IDE
lblid.Font = New Font("Arial',10,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.
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:
However, this will create the font as Arial, regardless of what it was before.Code:lblId.Font = New Font("Arial", 10, FontStyle.Bold)
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
Furthermore, you can use bitwise combinations for the FontStyle. For example, to set a font as Bold and Italic, you useCode:lblId.Font = New Font(lblId.Font, FontStyle.Bold)
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, FontStyle.Bold 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 styleCode:lblId.Font = New Font(lblId.Font, lblId.Font.FontStyle Or FontStyle.Italic)
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 And Not FontStyle.Bold)
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.Code:lblId.Font = New Font(lblId.Font, lblId.Font.FontStyle Xor FontStyles.Underline)
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.
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: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:1 = 00000001
2 = 00000010
4 = 00000100
8 = 00001000
16 = 00010000
32 = 00100000
64 = 01000000
128 = 10000000
Code:32 00100000
4 00000100
----------- OR
36 00100100
True. I was just afraid the post might already be gibberish to someone who doesn't know about bitwise operations, so I tried to keep it as simple as possible.