如何在RichTextBox里面,实现加粗,斜体和下划线功能?就是用户选定一段文字后,点击三个按钮,分别实现加粗,斜体和下划线功能。还有,C#做出来的程序,当字体同时有加粗,斜体和下划线格式。那么程序会不会出错?

解决方案 »

  1.   


    private void button1_Click(object sender, EventArgs e)
            {
                richTextBox1.SelectionFont = new Font(Font, FontStyle.Bold); // 加粗
            }        private void button2_Click(object sender, EventArgs e)
            {
                richTextBox1.SelectionFont = new Font(Font, FontStyle.Italic); // 斜体
            }        private void button3_Click(object sender, EventArgs e)
            {
                richTextBox1.SelectionFont = new Font(Font, FontStyle.Underline); // 下划线
            }        private void button4_Click(object sender, EventArgs e)
            {
                richTextBox1.SelectionFont = new Font(Font, 
                    FontStyle.Italic | FontStyle.Underline | FontStyle.Bold); // 加粗且斜体下划线
            }当字体同时有加粗,斜体和下划线格式,不会出错
      

  2.   


            FontStyle fontStyle;
            private void button1_Click(object sender, EventArgs e)
            {
                fontStyle = (FontStyle)(fontStyle | FontStyle.Bold);
                SetFont(fontStyle);
            }        private void button2_Click(object sender, EventArgs e)
            {
                fontStyle = (FontStyle)(fontStyle | FontStyle.Italic);
                SetFont(fontStyle);
            }        private void button3_Click(object sender, EventArgs e)
            {
                fontStyle = (FontStyle)(fontStyle | FontStyle.Underline);
                SetFont(fontStyle);
            }        private void SetFont(FontStyle style)
            {
                richTextBox1.SelectionFont = new Font("Arial",richTextBox1.SelectionFont.Size, fontStyle);
            }
      

  3.   

    gisyellow,谢谢了。当时已经结帖了,没有看到你的帖子。不好意思。