Making specific Text Boldefaced in a TextBox

前端 未结 7 1985
情歌与酒
情歌与酒 2020-11-28 12:37

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

7条回答
  •  囚心锁ツ
    2020-11-28 13:27

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

    /// 
    /// This method highlights the assigned text with the specified color.
    /// 
    /// The text to be marked.
    /// The new Backgroundcolor.
    /// The RichTextBox.
    /// The zero-based starting caracter position.
    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
        { }
    }
    

提交回复
热议问题