Reputation: 75
I have a textbox where I have some text. Using 3 Checkboxes I wanna change the style of the text so that if I select underline and bold the text becomes undelined and bold at the same time, but I don't know how to add a style property to the font without changing the previous style. Any idea on how I can do that? Thanks.
Upvotes: 1
Views: 4675
Reputation: 391276
To enable one of the font styles:
tb.Font = new Font(tb.Font, tb.Font.Style | FontStyle.Bold);
To disable one of the font styles:
tb.Font = new Font(tb.Font, tb.Font.Style & ~FontStyle.Bold);
Both of these will keep the other flags. See the bottom of this answer for why this works.
To set or clear the flag depending on a checkbox, you can use this code style:
var style = tb.Font.Style & ~FontStyle.Bold; // first remove it
if (checkbox.Checked)
style |= FontStyle.Bold; // add it if needed
In all three examples above, replace .Bold
with the appropriate style values.
Here's a small LINQPad program that demonstrates:
void Main()
{
var c = new UserControl();
var chkBold = new CheckBox();
var chkItalic = new CheckBox();
var chkUnderline = new CheckBox();
var tb = new TextBox();
c.Controls.AddRange(new Control[] { chkBold, chkItalic, chkUnderline, tb });
chkBold.Location = new Point(8, 8);
chkBold.Text = "Bold";
chkBold.CheckedChanged += (sender, e) =>
{
var style = tb.Font.Style & ~FontStyle.Bold;
if (chkBold.Checked)
style |= FontStyle.Bold;
tb.Font = new Font(tb.Font, style);
};
chkItalic.Location = new Point(8, 32);
chkItalic.Text = "Italic";
chkItalic.CheckedChanged += (sender, e) =>
{
var style = tb.Font.Style & ~FontStyle.Italic;
if (chkItalic.Checked)
style |= FontStyle.Italic;
tb.Font = new Font(tb.Font, style);
};
chkUnderline.Location = new Point(8, 56);
chkUnderline.Text = "Underline";
chkUnderline.CheckedChanged += (sender, e) =>
{
var style = tb.Font.Style & ~FontStyle.Underline;
if (chkUnderline.Checked)
style |= FontStyle.Underline;
tb.Font = new Font(tb.Font, style);
};
tb.Location = new Point(8, 80);
tb.Text = "Try it";
c.Dump();
}
The reason these two tricks work is that the FontStyle enum is tagged with Flags
and that signals that the enum is specially constructed to allow for this. Note that the Flags
attribute is not needed to write the above code, but its presence tells me that it will work.
The reason is that the enum has been set up as a bit by bit value, where each font style is given its own single bit that signals whether the style is present or not.
That means that this:
tb.Font = new Font(tb.Font, tb.Font.Style | FontStyle.Bold);
OR's in one new bit, setting it to 1, leaving all other bits untouched.
And this:
tb.Font = new Font(tb.Font, tb.Font.Style & ~FontStyle.Bold);
AND's the font style with the inverse of the font style, this might need some explanation.
This:
~FontStyle.Bold
takes the value, containing 1 bit set to 1, the bit assigned to the Bold style, and negates/inverses the entire value, setting all bits to their opposite value.
This means that all the bits of this will be set, except the one assigned to the Bold style.
Then, when we AND this with the existing font style value, we effectively remove that bit, and leave all other bits untouched.
Upvotes: 1
Reputation: 26199
Try This:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//bold checkbox is changed
if (checkBox1.Checked && checkBox2.Checked)
{
textBox1.Font = new Font(textBox1.Font.FontFamily,textBox1.Font.Size, ((FontStyle)((FontStyle.Bold | FontStyle.Underline))));
}
else if (checkBox1.Checked)
{
textBox1.Font = new Font(textBox1.Font.FontFamily,textBox1.Font.Size, FontStyle.Bold);
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
//unerline checkbox is changed
if (checkBox1.Checked && checkBox2.Checked)
{
textBox1.Font = new Font(textBox1.Font.FontFamily,textBox1.Font.Size, ((FontStyle)((FontStyle.Bold | FontStyle.Underline))));
}
else if (checkBox2.Checked)
{
textBox1.Font = new Font(textBox1.Font.FontFamily,textBox1.Font.Size, FontStyle.Bold);
}
}
Upvotes: 0
Reputation: 745
You can write css style and apply that class to textbox property cssClass
Upvotes: 0