Answer the question
In order to leave comments, you need to log in
How to set label font from settings file with less code?
Application in C# + WPF
There are 8 labels and 20 fonts
When the program is loaded, the font names are taken from the settings file and, depending on the font names, each label is assigned the appropriate font
I have if else conditions, everything works, but the code is sooo much, I would like refactoring to do, and to make the code smaller in some other way
How can this be done?
//переменные в которые помещаются названия шрифтов для каждого лейбла
var fontCategory1 = Settings.Default.LabelCategoryFont1;
var fontCategory2 = Settings.Default.LabelCategoryFont2;
var fontCategory3 = Settings.Default.LabelCategoryFont3;
var fontCategory4 = Settings.Default.LabelCategoryFont4;
var fontCategory5 = Settings.Default.LabelCategoryFont5;
var fontCategory6 = Settings.Default.LabelCategoryFont6;
var fontCategory7 = Settings.Default.LabelCategoryFont7;
var fontCategory8 = Settings.Default.LabelCategoryFont8;
if (fontCategory1 == "Heron Sans Light")
{
ApplyFontProperties(CategoryLabel1, new FontFamily("HeronSans"), FontWeights.Light,
FontStyles.Normal);
}
if (fontCategory2 == "Heron Sans Light")
{
ApplyFontProperties(CategoryLabel2, new FontFamily("HeronSans"), FontWeights.Light,
FontStyles.Normal);
}
if (fontCategory1 == "Heron Sans Bold")
{
ApplyFontProperties(CategoryLabel1, new FontFamily("HeronSans"), FontWeights.Bold,
FontStyles.Normal);
}
if (fontCategory2 == "Heron Sans Bold")
{
ApplyFontProperties(CategoryLabel2, new FontFamily("HeronSans"), FontWeights.Bold,
FontStyles.Normal);
}
private void ApplyFontProperties(Label label, FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle)
{
if (label == CategoryLabel1)
{
CategoryLabel1.FontFamily = fontFamily;
CategoryLabel1.FontStyle = fontStyle;
CategoryLabel1.FontWeight = fontWeight;
}
if (label == CategoryLabel2)
{
CategoryLabel2.FontFamily = fontFamily;
CategoryLabel2.FontStyle = fontStyle;
CategoryLabel2.FontWeight = fontWeight;
}
if (label == CategoryLabel3)
{
CategoryLabel3.FontFamily = fontFamily;
CategoryLabel3.FontStyle = fontStyle;
CategoryLabel3.FontWeight = fontWeight;
}
if (label == CategoryLabel4)
{
CategoryLabel4.FontFamily = fontFamily;
CategoryLabel4.FontStyle = fontStyle;
CategoryLabel4.FontWeight = fontWeight;
}
if (label == CategoryLabel5)
{
CategoryLabel5.FontFamily = fontFamily;
CategoryLabel5.FontStyle = fontStyle;
CategoryLabel5.FontWeight = fontWeight;
}
if (label == CategoryLabel6)
{
CategoryLabel6.FontFamily = fontFamily;
CategoryLabel6.FontStyle = fontStyle;
CategoryLabel6.FontWeight = fontWeight;
}
if (label == CategoryLabel7)
{
CategoryLabel7.FontFamily = fontFamily;
CategoryLabel7.FontStyle = fontStyle;
CategoryLabel7.FontWeight = fontWeight;
}
if (label == CategoryLabel8)
{
CategoryLabel8.FontFamily = fontFamily;
CategoryLabel8.FontStyle = fontStyle;
CategoryLabel8.FontWeight = fontWeight;
}
<setting name="LabelCategoryFont1" serializeAs="String">
<value>Heron Sans Light</value>
</setting>
<setting name="LabelCategoryFont2" serializeAs="String">
<value>Heron Sans Bold</value>
</setting>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question