📜  c# fontweight in code - C# (1)

📅  最后修改于: 2023-12-03 14:59:40.037000             🧑  作者: Mango

C# FontWeight in Code

In C#, FontWeight property is used to get or set the weight or thickness of a font. The FontWeight property is of type FontWeight structure and can be set to one of the following values:

  • Thin: Represents the thinnest font weight.
  • ExtraLight: Represents an extra-light font weight.
  • UltraLight: Represents an ultra-light font weight.
  • Light: Represents a light font weight.
  • Regular: Represents a regular font weight.
  • Medium: Represents a medium font weight.
  • DemiBold: Represents a demi-bold font weight.
  • SemiBold: Represents a semi-bold font weight.
  • Bold: Represents a bold font weight.
  • ExtraBold: Represents an extra-bold font weight.
  • UltraBold: Represents an ultra-bold font weight.
  • Black: Represents the blackest font weight.
Setting FontWeight in Code

To set the FontWeight property of a control in code, you can use the following syntax:

control.FontStyle = FontWeights.Bold;

where control is the instance of the control whose FontWeight property you want to set and FontWeights.Bold is the desired FontWeight value.

Getting FontWeight in Code

To get the FontWeight property value of a control in code, you can use the following syntax:

var fontWeight = control.FontWeight;

where control is the instance of the control whose FontWeight property you want to get, and fontWeight is the value of the FontWeight property.

Examples

Here are some examples of setting FontWeight property in code:

Setting the FontWeight of a TextBlock control to Bold
TextBlock1.FontWeight = FontWeights.Bold;
Setting the FontWeight of a Button control to ExtraBold
Button1.FontWeight = FontWeights.ExtraBold;
Conclusion

In conclusion, FontWeight property is a useful property to set the weight or thickness of a font in C# code. By using this property, you can make sure that your application's fonts are displayed exactly as you want them to be.