使得一个TextBox粗体特定文本 [英] Making specific Text Bolded in a TextBox

查看:117
本文介绍了使得一个TextBox粗体特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我目前有打印出的信息时,他们preSS不同势按钮用户texbox。我不知道是否有一种方法,使只有一些我的文字而其余的心不是粗体的。

Hi I currently have a texbox that prints out info to the user when they press diffrent buttons. I was wondering if there was a way to make only some of my text bolded while the rest isnt.

香港专业教育学院尝试了以下内容:

Ive tried the following:

textBox1.FontWeight = FontWeights.UltraBold;
textBox1.Text. = ("Your Name: " );
TextBox1.FontWeight = FontWeights.Regular;
textBox1.Text += (nameVar);

唯一的问题是,使用这种方式要么让一切大胆的或没有。
有没有办法做到这一点?在C#中使用即时通讯WPF项目

Only problem is that using this way will either make everything bold or nothing. Is there a way to do this? Im using WPF project in C#

任何意见或建议,AP preciated。
谢谢!

Any Comments or suggestions are appreciated. Thanks!

编辑:所以现在我尝试做富文本框,你所有的建议,但我似乎无法得到任何东西出现在它:

So now im trying to do the RichText box that you all suggested but I cant seem to get anything to appear in it:

// Create a simple FlowDocument to serve as the content input for the construtor.
FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run("Simple FlowDocument")));
// After this constructor is called, the new RichTextBox rtb will contain flowDoc.
RichTextBox rtb = new RichTextBox(flowDoc);

RTB是我的RichTextBox的我在WPF中创建的名称

rtb is the name of my richtextbox i created in my wpf

感谢

推荐答案

使用一个RichTextBox,下面,我已经写了这个问题的方法 - 希望它帮助; - )

use a RichTextBox, below a method that i have wrote for this problem - hope it helps ;-)

/// <summary>
/// This method highlights the assigned text with the specified color.
/// </summary>
/// <param name="textToMark">The text to be marked.</param>
/// <param name="color">The new Backgroundcolor.</param>
/// <param name="richTextBox">The RichTextBox.</param>
/// <param name="startIndex">The zero-based starting caracter position.</param>
public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox, int startIndex)
{
    if (startIndex < 0 || startIndex > textToMark.Length-1) startIndex = 0;

    System.Drawing.Font newFont = new Font("Verdana", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 178, false);
    try
    {               
        foreach (string line in richTextBox.Lines)
        { 
            if (line.Contains(textToMark))
            {
                richTextBox.Select(startIndex, line.Length);
                richTextBox.SelectionBackColor = color;
            }
            startIndex += line.Length +1;
        }
    }
    catch
    { }
}

这篇关于使得一个TextBox粗体特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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