Substract Flag From FontStyle (Toggling FontStyles) [C#]

后端 未结 1 1811
青春惊慌失措
青春惊慌失措 2020-12-10 05:42

I have a little problem. I have one 1 RichTextBox and 2 Buttons.

I have that 2 buttons for \"toggle Bold FStyle\" and \"toggle Italic FStyle\".

I want to tog

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 06:27

    The easiest way is to use bitwise XOR (^), which just toggles the value:

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.SelectionFont = new Font(richTextBox1.Font,
            richTextBox1.SelectionFont.Style ^ FontStyle.Bold);
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        richTextBox1.SelectionFont = new Font(richTextBox1.Font,
            richTextBox1.SelectionFont.Style ^ FontStyle.Italic);
    }
    

    0 讨论(0)
提交回复
热议问题