Reputation: 7293
I am using this code to make all my textboxes the same font:
if (textBox1.Font.Underline)
{
foreach (Control y in this.Controls)
{
if (y is TextBox)
{
((TextBox)(y)).Font = new Font(((TextBox)(y)).Font, FontStyle.Regular);
}
}
}
else
{
foreach (Control y in this.Controls)
{
if (y is TextBox)
{
((TextBox)(y)).Font = new Font(((TextBox)(y)).Font, FontStyle.Underline);
}
}
Lets say i click the bold button. The text will turn Bold. When i click the underline button, the text should be Bold and underlined, but it is only underlined??? WHY?
Upvotes: 0
Views: 4196
Reputation: 54562
FontStyle
is an enumeration you can Or
them together to add or Xor
to remove.
i.e.
to add underlining to existing style:
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Underline);
to remove underlining from style:
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Underline);
and you can check for which Enumerations are in the Font.Style by doing this.
if ((textBox1.Font.Style.HasFlag(FontStyle.Underline)))
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Underline);
}
else
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Underline);
}
Upvotes: 8
Reputation: 13599
you can try using something like this
List<Control> controls = Controls.OfType<TextBox>().Cast<Control>().ToList();
foreach (Control m in controls)
{
if (m.Font.Bold)
{
m.Font = new Font(m.Font, FontStyle.Underline);
}
else
{
m.Font = new Font(m.Font, FontStyle.Bold);
m.Font = new Font(m.Font, FontStyle.Underline);
}
}
Upvotes: 1
Reputation: 12440
Instead of
((TextBox)(y)).Font = new Font(((TextBox)(y)).Font, FontStyle.Underline);
use
((TextBox)(y)).Font = new Font(((TextBox)(y)).Font.FontFamily, ((TextBox)(y)).Font.Size, FontStyle.Underline);
Upvotes: 0