Making richtextbox bold in C# .Net - c#

I'm trying to make bold text in richTextBox. Text I want to make bold is in a second row "Cpu usage" in code.
Can you edit this code to make it bold?
double cpuUsage = Math.Round(perfCpuCount.NextValue(), 2);
MonitorLog.AppendText(string.Format("Cpu usage: {0}%\r\n\n", cpuUsage));

Change the SelectionFont to include the bold style, and write the text that should be bold, then switch it back to regular:
MonitorLog.SelectionFont = new Font(MonitorLog.Font, FontStyle.Bold);
MonitorLog.AppendText("CPU Usage:");
MonitorLog.SelectionFont = new Font(MonitorLog.Font, FontStyle.Regular);
MonitorLog.AppendText(string.Format(" {0}%\r\n\n", cpuUsage));

Related

Change color in RichTextBox

I am using this simple example from MSDN
to insert lines in a RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
I want to apply a chosed color to the lines of text, but how to do it?
The Paragraph or Run classes doesn't have any direct method to change the color.
EDIT
I don't want to use all the awkard SelectionStart, SelectionEnd stuff as posted on the linked post!.
My case is different and is much more simple: the solution posted from mm8 explains it and is very elegant.
One single line of code and that is!
Please see the answer!
The Paragraph or Run classes doesn't have any direct method to change the color.
The Run class inherits from TextElement and this class has a Foreground property that you can set to a Brush:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });
You can get/set text color via Foreground property of the rich text box. As bellow example, I changed the text color of rich text box to blue:
myRichTextBox.Foreground = Brushes.Blue;
Happy coding!

changing font style of textbox

I want to change font style for a textbox when I click the button.For this my code is below and it is okay;
protected void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Font.Size = FontUnit.XLarge;
TextBox1.ForeColor = System.Drawing.Color.Crimson;
TextBox1.BackColor = System.Drawing.Color.Snow;
TextBox1.BorderColor = System.Drawing.Color.HotPink;
}
But Can I do where I select of write which is in the textbox it changes only that part.For example textbox1.Text="Computer Programs" and the user select only "Computer" part of textbox1.Only "Computer" part must be changed.
From the sounds of it you want to format only part of the text? For that you'll need to look at a RichTextBox control.
With a RichTextBox you can make use of text selections and set formatting on only those areas:
RichTextBox rich = new RichTextBox();
rich.Text = "Here is some text for the Rich Text Box";
rich.SelectionStart = 0;
rich.SelectionLength = 4;
rich.SelectionFont = new Font(rich.Font, FontStyle.Bold);
After you're done setting your font to your selection you should set the font immediately afterwards back to the original so that you don't continue the style outside the area you wanted to apply it:
rich.SelectionStart = rich.SelectionStart + rich.SelectionLength;
rich.SelectionLength = 0;
rich.SelectionFont = rich.Font;
This should result in "Here is some text for the Rich Text Box" changing to look like: "Here is some text for the Rich Text Box".

How Can Format Text Inside TextBoxes In WinForms

as you know in web applications there are some third party libraries that we can use for convert a text box to an editor like below :
tinymce
but what about win forms?
how can i format text inside Text Boxes in win forms?
i mean i want to convert regular text boxes in a form to an editor by setting a property(is it possible or not?).
how can i do that?
public void CreateMyRichTextBox()
{
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Dock = DockStyle.Fill;
richTextBox1.LoadFile("C:\\MyDocument.rtf");
richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SaveFile("C:\\MyDocument.rtf", RichTextBoxStreamType.RichText);
this.Controls.Add(richTextBox1);
}

RichTextBox font set to all lines in C#

I changed the font of a RichTextBox, but this change does not set to all of its characters properly. Indeed there exist two font for the RichTextBox and that is not beautiful. Is there a solution for this problem!?
This code does not work correctly:
this.richTextBox1.Font = new System.Drawing.Font("Maiandra GD", 12);
Set the selection to the entire box and set the SelectionFont.
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength = this.richTextBox1.SelectionLength;
this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 12);

How can i change the background color of datagridview headercell in code using C#?

I want to change the backcolor of a headercell in datagridview row .So how can I do that in code C#?
Also, how do I change font properties of the text named as value of the headercell like headercell.value?
You can do this in the properties window. Set EnableHeadersVisualStyles to false then make the changes to the font, cell color, alignment, etc in ColumnHeadersDefaultCellStyle.
To change the font properties of a specified header cell you can try:
dataGridView1.Columns[index].HeaderCell.Style.Font = new Font("Arial", 24, , FontStyle.Bold);
you can try this
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.Columns[index].HeaderCell.Style.Font = new Font("Arial", 24, , FontStyle.Bold);
I've figured out how to change the backcolor using:
dataGridView1.Rows[index].HeaderCell.Style.BackColor = System.Drawing.Color.Red;
I still don't know the answer to my second question.

Categories