简体   繁体   中英

Set text style in a textbox

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.

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();
}

Why does these two tricks work?

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.

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);
        }
    }

您可以编写css样式并将该类应用于文本框属性cssClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM