Making specific Text Boldefaced in a TextBox

前端 未结 7 1946
情歌与酒
情歌与酒 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:35

    Taking jwillmer's excellent example, I made some adjustments because it was coloring the entire error line for me:

        public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox)
        {
            int startIndex = 0;
    
            string text = richTextBox.Text;
            startIndex = text.IndexOf(textToMark);
    
            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, textToMark.Length);
                        richTextBox.SelectionColor = color;
                        richTextBox.SelectionFont = newFont;
                    }
                }
            }
            catch{ }
        }
    

    Also, I added unique tags before and after the text to color to get the text, then removed them.

    0 讨论(0)
提交回复
热议问题