Making specific Text Boldefaced in a TextBox
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
{ }
}
Set specific text to bold in WPF RichTextBox
After several tests, I figured out a simple solution.
CaretPosition = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
This set caret in the right orientation, preventing the BOLD setting from continuing within the Run object.
if(textPointerEnd.GetNextInsertionPosition(LogicalDirection.Forward) == null)
new Run("", textPointerEnd);
This would add a Run object to the end of a new Bold object that was located at the end of the Paragraph object.
Change font style to richtextbox
You need to set Bold in FontWeight-property:
sp.FontWeight = FontWeights.Bold;
Same in WinForms:
sp.Font = new Font(sp.Font.Name, sp.Font.Size, FontStyle.Bold);
Related Topics
Check If a Property Exists in a Class
Getting the Index of a Particular Item in Array
JSON.Net: Specify Converter for Dictionary Keys
"The Linq Expression Node Type 'Invoke' Is Not Supported in Linq to Entities" - Stumped!
..The Underlying Connection Was Closed: an Unexpected Error Occurred on a Receive
Will Using Linq to SQL Help Prevent SQL Injection
Are Ienumerable Linq Methods Thread-Safe
How to Get All Cookies of a Cookiecontainer
How to Write a Transaction to Cover Moving a File and Inserting Record in Database
Copy Values from One Object to Another
Exporting Datagridview to CSV File
Why Won't Anyone Accept Public Fields in C#
Entity Framework Ef.Functions.Like VS String.Contains
Why Does Binarywriter Prepend Gibberish to the Start of a Stream? How to Avoid It