Changing the FontStyle

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

I want to change the FontStyle in a DataGridView control. But for
some reason, Font.Strikeout is read only. The only thing I can think
of is to create a new Font with the Strikeout FontStyle. Is that
right?

Dom
 
I want to change the FontStyle in a DataGridView control. But for
some reason, Font.Strikeout is read only. The only thing I can think
of is to create a new Font with the Strikeout FontStyle. Is that
right?

Dom

I believe thats correct. I actually got mad in class one time and
started to create a font object that had those properties not set to
readonly so I could change the boolean back and forth thus making my
coding experience easier.
 
I want to change the FontStyle in a DataGridView control. But for
some reason, Font.Strikeout is read only. The only thing I can think
of is to create a new Font with the Strikeout FontStyle. Is that
right?

Yes. Fonts are immutable, like Strings. If you want a Font that's just
like an existing one but with something changed, you have to create a new
Font instance based on the existing one, but with the thing(s) you want
changed.

For example, assuming an existing font referenced by a variable
"fontExisting", you can set the strikeout style like this:

Font fontNew = new Font(fontExisting, FontStyle.Strikeout);

Pete
 
Fonts must be constructed from scratch whenever a style change is required.
This is because the font engine recalculates a lot of complex data every
time a font is declared.

To update a font in any control, assign a newly constructed font or one that
you've created earlier and stored for the purpose.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top