In Windows Forms, you cannot directly apply formatting, such as bold text, to individual characters or lines within a multiline TextBox control. However, you can achieve a similar effect by using a RichTextBox control instead. The RichTextBox control supports various formatting options, including bold text.
Here’s an example of how you can add bold text to a multiline RichTextBox control in WinForms:
- Add a RichTextBox control to your form. You can do this by dragging and dropping the RichTextBox control from the Toolbox onto your form.
- Set the multiline property of the RichTextBox control to true. You can do this either in the designer properties window or programmatically in the form’s constructor or Load event.
richTextBox1.Multiline = true;
To add bold text to the RichTextBox, you can use the
SelectionFont property along with the
Font class. Here’s an example that demonstrates how to add a bold text:
// Set the initial text in the RichTextBox richTextBox1.Text = "This is a multiline RichTextBox control."; // Select the desired text range where you want to apply bold formatting richTextBox1.Select(5, 2); // Start at index 5 and select 2 characters // Apply bold formatting to the selected text richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
- In the above example, the text “is” starting at index 5 and spanning 2 characters will be displayed in bold.
You can repeat the above steps to apply bold formatting to different parts of the text in the RichTextBox. Remember to set the
SelectionFont property each time before selecting a different range of text.
Note that the RichTextBox control offers many other formatting options, such as italic, underline, font size, etc. You can explore the various properties and methods available in the
RichTextBox class to achieve different formatting effects.