简体   繁体   中英

How to use combobox to change the richTextBox font size

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)//change font size
{          
    if (toolStripComboBox1.SelectedIndex == 0)
    {
        richTextBox1.SelectionFont = new Font("Comic Sans MS", 12);
    }

    if (toolStripComboBox1.SelectedIndex == 1)
    {
        richTextBox1.SelectionFont = new Font("Comic Sans MS", 19);
    }
}

Here is my code, in this situation I have to click "19" twice to make it work, what's error of my code

在此处输入图片说明

在此处输入图片说明

private void toolStripComboBox1_Click(object sender, EventArgs e)
        {
            toolStripComboBox1.ComboBox.SelectionChangeCommitted += ComboBoxOnSelectionChangeCommitted;
        }


private void ComboBoxOnSelectionChangeCommitted(object o, EventArgs eventArgs)
        {
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0:
                    richTextBox1.SelectionFont = new Font("Comic Sans MS", 12);
                    break;
                case 1:
                    richTextBox1.SelectionFont = new Font("Comic Sans MS", 19);
                    break;
                default:
                    richTextBox1.SelectionFont = new Font("Comic Sans MS", 9);
                    break;
            }
        }

You can also use if instead switch, but personaly i would prefer switch in this situation.

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