Making specific text bold in a string in C# Windows Forms
11,958
You can do this with the help of FontStyle
interface.
Just add a button in your form and name it Bold and create a click event for that.
You have to use RichTextBox
for this, you cannot do this with TextBox
.
This code will convert the selected text to bold.
private void btnBold_Click(object sender, EventArgs e)
{
FontStyle style = tbMessage.SelectionFont.Style;
if (tbMessage.SelectionFont.Bold)
{
style = style & ~FontStyle.Bold;
btnBold.Font = new Font(btnBold.Font, FontStyle.Regular);
}
else
{
style = style | FontStyle.Bold;
btnBold.Font = new Font(btnBold.Font, FontStyle.Bold);
}
tbMessage.SelectionFont = new Font(tbMessage.SelectionFont, style);
tbMessage.Focus();
}
Related videos on Youtube
Author by
Michael Naidis
Updated on June 04, 2022Comments
-
Michael Naidis about 2 years
I want to make part of the text bold in a textbox, for example the textbox contains.
"This is a text box"
So it will be "This is a text box"
How can I do it in C# Windows Forms?
-
Robert Langdon almost 10 yearsSo is it that only second word should be bold?
-
TheBetaProgrammer almost 10 yearsTry using html This '<b>is</b>' a text book
-
Michael Naidis almost 10 yearsIt's windows forms by the way, and not necessarily second, Robert - word of my choice.
-
user3613916 almost 10 yearsIs this a RichTextBox or just a normal TextBox?
-
-
Michael Naidis almost 10 yearsWhat if I want just a word from the message?
-
pooja_baraskar almost 10 yearsselect that word with the help of mouse and click on the bold button which has the event handler with code above. This will make the selected word Bold/Unbold. Just try this code once , you will understand.