将多种字体应用于richbox文本 [英] apply multiple font to richbox text

查看:79
本文介绍了将多种字体应用于richbox文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想给richboxtext提供不同的字体

我的代码是



 rtxtTemp.SelectionLength = rtxtTemp .SelectionLength;  //   1; //不确定这个 
rtxtTemp.SelectionFont = fnt;
rtxtTemp.SelectionBackColor = System.Drawing.Color.Aqua;
rtxtTemp.SelectedText = stTemp;
rtxtTemp.SelectionBackColor = System.Drawing.Color.White;
rtxtTemp.SelectionFont = orgFnt;
rtxtTemp.Font = fnt;







但它会给所有richboxtext提供相同的字体



任何建议

解决方案

这种类型的操作不必复杂:

 //在表单范围内
字体selFont1 =新字体(Arial,24.0F);
字体selFont2 =新字体(Arial,18.0F);
颜色fColor1 = Color.Blue;
颜色fColor2 = Color.Red;
颜色bColor1 = Color.LightGray;
颜色bColor2 = Color.AliceBlue;

private void setFontAndColors(int txtStyle)
{
switch(txtStyle)
{
case 1:
richTextBox1.SelectionFont = selFont1;
richTextBox1.SelectionBackColor = bColor1;
richTextBox1.SelectionColor = fColor1;
休息;
case 2:
richTextBox1.SelectionFont = selFont2;
richTextBox1.SelectionBackColor = bColor2;
richTextBox1.SelectionColor = fColor2;
休息;
}
}

private void button1_Click(object sender,EventArgs e)
{
setFontAndColors(1);
}

private void button2_Click(object sender,EventArgs e)
{
setFontAndColors(2);
}


第一行什么都不做。

带下划线的部分只是互相覆盖,而粗体是做同样的事情。您只是将theSelectionFont设置为fnt,然后立即将其设置为orgFnt。您需要选择不同的文本部分以将字体更改为不同。



当您设置第一个文本时,您似乎没有选择任何文本字体和颜色,所以没有什么可以显示它。



 rtxtTemp.SelectionLength = rtxtTemp.SelectionLength;  //   1; //不确定这个 
rtxtTemp.SelectionFont = fnt ;
rtxtTemp.SelectionBackColor = System.Drawing.Color.Aqua;
rtxtTemp.SelectedText = stTemp;
rtxtTemp.SelectionBackColor = System.Drawing.Color.White;
rtxtTemp.SelectionFont = orgFnt;
rtxtTemp.Font = fnt;


want to give different font to richboxtext
My code is

rtxtTemp.SelectionLength = rtxtTemp.SelectionLength;// 1; // not sure about this one
                rtxtTemp.SelectionFont = fnt;
                rtxtTemp.SelectionBackColor = System.Drawing.Color.Aqua;
                rtxtTemp.SelectedText = stTemp;
                rtxtTemp.SelectionBackColor = System.Drawing.Color.White;
                rtxtTemp.SelectionFont = orgFnt;
                rtxtTemp.Font = fnt;




but it will give same font to all richboxtext

any suggestions

解决方案

This type of operation does not have to complicated:

// in Form scope
Font selFont1 = new Font("Arial",24.0F);
Font selFont2 = new Font("Arial",18.0F);
Color fColor1 = Color.Blue;
Color fColor2 = Color.Red;
Color bColor1 = Color.LightGray;
Color bColor2 = Color.AliceBlue;

private void setFontAndColors(int txtStyle)
{
    switch(txtStyle)
    {
        case 1:      
            richTextBox1.SelectionFont = selFont1;
            richTextBox1.SelectionBackColor = bColor1;
            richTextBox1.SelectionColor = fColor1;
        break;
        case 2:      
            richTextBox1.SelectionFont = selFont2;
            richTextBox1.SelectionBackColor = bColor2;
            richTextBox1.SelectionColor = fColor2;
        break;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    setFontAndColors(1);
}

private void button2_Click(object sender, EventArgs e)
{
    setFontAndColors(2);
}


The first line does nothing.
The underlined sections are just overwriting one another, and the bold are doing the same thing. You are just setting theSelectionFont to fnt then immediately setting it to orgFnt. You need to select different sections of text to change the font to be different.

It also doesn't appear that you actually had any text selected when you set the first font and color, so there would be nothing to display it.

rtxtTemp.SelectionLength = rtxtTemp.SelectionLength;// 1; // not sure about this one
                rtxtTemp.SelectionFont = fnt;
                rtxtTemp.SelectionBackColor = System.Drawing.Color.Aqua;
                rtxtTemp.SelectedText = stTemp;
                rtxtTemp.SelectionBackColor = System.Drawing.Color.White;
                rtxtTemp.SelectionFont = orgFnt;
                rtxtTemp.Font = fnt;


这篇关于将多种字体应用于richbox文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆