Change color in RichTextBox - c#

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!

Related

WPF read Checkbox.Content

I have a couple of CheckBoxes with a TextBlock as content.
Now I want to read out the TextBlock.Text from each Checkbox.
If I read out the content like checkBox.Content.ToString(); I only get System.Windows.Controls.TextBlock which kinda makes sense.
I also tried to create a new TextBlock and give it the content but it didn't work.
TextBlock _tempTBL = new TextBlock();
_tempTBL = checkBox.Content;
Any help is much appreciated.
var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text
Edit:
On a side note, you can directly set desired text as CheckBox's content.
checkBox.Content = "Hello World";
And when you want to access the text, no type cast is needed
string text = checkBox.Content;
You have to cast the type to a TextBlock:
// no need to 'new' it up if you're assigning an existing instance...
TextBlock _tempTBL = (TextBlock) checkBox.Content;

Making richtextbox bold in C# .Net

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));

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".

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);

Using C# & Powerpoint OpenXML, is it possible to change the font size and color of text

I am using openXML and C# to generate a powerpoint slide but I can't seem to figure out how to change / set the text size and color. Is this possible and are there any example as I can't seem to find any with googling?
I am building out a table (similar to this: http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx) and I want to change a number of things in each cell (fontsize, font color, backcolor of cell).
Your comments state this Formatting is for a Table inside a PowerPoint slide.
Assumptions
I am assuming you have already created the table, table rows, table cells, and display text.
Also assuming you have everything working and now you want to add formatting.
If you want to format your Text and Cells, you may do so using the following:
//Create the TableCell for the PowerPoint table you are building.
A.TableCell tableCell3 = new A.TableCell();
A.TextBody textBody5 = new A.TextBody();
A.BodyProperties bodyProperties5 = new A.BodyProperties();//Created but not modified.
A.ListStyle listStyle5 = new A.ListStyle();//Created but not modified.
A.Paragraph paragraph5 = new A.Paragraph();
//First Word: "Hello" with Font-Size 60x and Font-Color Green.
A.Run run1 = new A.Run();
A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false, SmartTagClean = false };//Set Font-Size to 60px.
A.SolidFill solidFill1 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00B050" };//Set Font-Color to Green (Hex "00B050").
solidFill1.Append(rgbColorModelHex1);
runProperties1.Append(solidFill1);
A.Text text1 = new A.Text();
text1.Text = "Hello";
run1.Append(runProperties1);
run1.Append(text1);
//Second Word: "World" with Font-Size 60x and Font-Color Blue.
A.Run run2 = new A.Run();
A.RunProperties runProperties2 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false, SmartTagClean = false };//Set Font-Size to 60px.
A.SolidFill solidFill2 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex2 = new A.RgbColorModelHex() { Val = "0070C0" };//Set Font-Color to Blue (Hex "0070C0").
solidFill2.Append(rgbColorModelHex2);
runProperties2.Append(solidFill2);
A.Text text2 = new A.Text();
text2.Text = " World";
run2.Append(runProperties2);
run2.Append(text2);
//This element specifies the text run properties that are to be used if another run is inserted after the last run specified.
//This effectively saves the run property state so that it can be applied when the user enters additional text.
//If this element is omitted, then the application can determine which default properties to apply.
//It is recommended that this element be specified at the end of the list of text runs within the paragraph so that an orderly list is maintained.
// Source: http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.endparagraphrunproperties.aspx
//Set the default formatting for words entered after "Hello World" with Font-Size 60x and Font-Color Blue.
A.EndParagraphRunProperties endParagraphRunProperties5 = new A.EndParagraphRunProperties() { Language = "en-US", FontSize = 6000, Dirty = false };//Set Font-Size to 60px.
A.SolidFill solidFill3 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex3 = new A.RgbColorModelHex() { Val = "0070C0" };//Set Font-Color to Blue (Hex "0070C0").
solidFill3.Append(rgbColorModelHex3);
endParagraphRunProperties5.Append(solidFill3);
paragraph5.Append(run1);//Append Run: "Hello".
paragraph5.Append(run2);//Append Run: " World".
paragraph5.Append(endParagraphRunProperties5);//Append formmatting for any text the user may enter after the words "Hello World".
textBody5.Append(bodyProperties5);//Created but not modified.
textBody5.Append(listStyle5);//Created but not modified.
textBody5.Append(paragraph5);//Append Paragraph: "Hello World"
//TableCell Properties. Set Background-Color to Red (Hex "FF0000").
A.TableCellProperties tableCellProperties3 = new A.TableCellProperties();
A.SolidFill solidFill4 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex4 = new A.RgbColorModelHex() { Val = "FF0000" };//Red Background for Single TableCell.
solidFill4.Append(rgbColorModelHex4);
tableCellProperties3.Append(solidFill4);//Append Red Background.
tableCell3.Append(textBody5);
tableCell3.Append(tableCellProperties3);
I cheated and used the "Open XML SDK 2.0 Productivity Tool for Microsoft Office".
I simply created a new PowerPoint file, added a table, and edited the 3rd Cell.
Then I ran the SDK tool and reflected the code on "[]/ppt/presentation.xml".
I added comments to the reflected code so that you may understand it better.
As another user pointed out this is possible in ML. Here is a solution I used to solve this problem:
// Assume we are adding a A.TableCell to A.TableRow...
A.TableCell tc = new A.TableCell(
new A.TextBody(
new A.BodyProperties(),
new A.Paragraph(new A.Run(
// -> Add the RunProperties as additional Element to A.Run constructor:
new A.RunProperties() { FontSize = 600 }, new A.Text("some text") ) ) ),
new A.TableCellProperties() );
// Now add the cell to a A.TableRow instance...
When creating an A.TableCell cell for appending to the row of an A.Table, I added a RunProperty Element to the A.Run nesting the A.Text for the cell, and I instantiated it with the FontSize attribute set accordingly: { FontSize = 600 }.
Hope that helps someone.
Once you have the object for the run or paragraph you wish to manipulate, you can add whatever styling you want to the run or paragraph properties.

Categories