RichTextBlock paragraphs background color - c#

I have a RichTextBlock with a couple of paragraphs. I need some paragraphs to have a background color. I cannot find the Background property on Paragraph or Run. How do I do this? In WPF there is a Block but it does not seem to be present in WinRT.

It is not possible but it is Possible to insert a InlineUIContainer with a StackPanel with a background.

You'll need to get the index for the start of the paragraph and for the end of the paragraph and then use:
//Select the line from it's number
int startIndex = richTextBox.GetFirstCharIndexFromLine(lineNumber);
richTextBox.Select(startIndex, length);
//Set the selected text fore and background color
richTextBox.SelectionColor = System.Drawing.Color.White;
richTextBox.SelectionBackColor= System.Drawing.Color.Blue;
And you could also look at ScintillaNET for a nice Text Editing Control.

Related

Is it possible to make text centered in richtextbox C# [duplicate]

How do I align the text in a RichTextBox?
Basically, the RTB contains:
"--testing"
"--TESTING"
"TESTING--"
"testing--"
Which all have the same number of characters, but have different alignments. How can I align them properly? Im fairly new to C# and confused since it aligned properly in Java's TextArea.
Thank you!
You want to use the RichTextBox.SelectionAlignment property.
For instance if you want the whole textbox centered, then you would do:
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
If you want only part of the textbox with a certain alignment, then use the RichTextBox.Select() routine to select the text, then set the SelectionAlignment property.
You would have to change the font to a monospaced font, like Courier. This behavior you're showing is standard with most fonts, as not all characters are the same width.
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.DeselectAll();
Unless it is very necessary for you to use a rich textbox, you can simply use a textbox and choose alignment as
textbox.TextAlign = HorizontalAlignment.Center;/*could be left, right or center*/
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center;
or
GetRichTextBox().SelectionAlignment = HorizontalAlignment.Center;
for multiple pages.

Adding backround color to certain part of text with MIGRADOC AND PDFSHARP

I'm struggling to add background color on certain part of sentence(text), word with MIGRADOC and PDFSHARP. Any suggestions how to do ?
par.addText(coloredText);
This is how I tried to add text that should be colored but there is no way to setup for color, except for paragraph (paragraph.shading.color = Color.red) but I need part of the text in the paragraph.
Thanks
With FormattedText it is possible to determine the color of the text (unfortunately not the background)
With the piece of code below it is possible to do this:
Paragraph par = section.AddParagraph();
par.Format.Alignment = ParagraphAlignment.Left;
// Use formatted text to specify the color
FormattedText ftext = new FormattedText();
ftext.AddText("Coloured Text");
ftext.Color = Colors.Red;
par.AddText("normal Text");
par.AddSpace(1);
par.Add(ftext);
par.AddSpace(1);
par.AddText("rest of the normal Text");

How do I highlight a certain word in a label?

I have been watching a couple of videos and I've noticed that you can highlight or set the BackColor for every word it finds in a RichTextBox, I tried doing this with a Label which is what I'm working with on my project but I can't use Label.Find and Label.SelectionBackColor etc. Is there a way I could search a word in my label and highlight it?
You cannot use two different foreground/background colors in a label. You might split up the text in different labels or just use a richtextbox.
Here is a workaround, create a RichTextBox and use it as a label.
Set these properties to make it look like a label:
richTextBox.ReadOnly = true;
richTextBox.BorderStyle = BorderStyle.None;
richTextBox.BackColor = SystemColors.Control; // or whatever your background color is
work around to disable User selection:
richTextBox.Enabled = false;
richTextBox.SelectAll();
richTextBox.SelectionColor = SystemColors.ControlText; // or whatever you want the default text color to be
// you have to set the color or else it will be gray because of Enabled=false
Edit: i just tried it, after SelectAll(); and SelectionColor = SystemColors.ControlText any changing or adding of Text keeps it black (unless the current SelectionStart is at a point of the text where the color is different

How to 'align' text in RichTextBox C#?

How do I align the text in a RichTextBox?
Basically, the RTB contains:
"--testing"
"--TESTING"
"TESTING--"
"testing--"
Which all have the same number of characters, but have different alignments. How can I align them properly? Im fairly new to C# and confused since it aligned properly in Java's TextArea.
Thank you!
You want to use the RichTextBox.SelectionAlignment property.
For instance if you want the whole textbox centered, then you would do:
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
If you want only part of the textbox with a certain alignment, then use the RichTextBox.Select() routine to select the text, then set the SelectionAlignment property.
You would have to change the font to a monospaced font, like Courier. This behavior you're showing is standard with most fonts, as not all characters are the same width.
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.DeselectAll();
Unless it is very necessary for you to use a rich textbox, you can simply use a textbox and choose alignment as
textbox.TextAlign = HorizontalAlignment.Center;/*could be left, right or center*/
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center;
or
GetRichTextBox().SelectionAlignment = HorizontalAlignment.Center;
for multiple pages.

Change link color in RichTextBox

I have a RichTextBox which contains links posted by the users.
The problem is that my RTB makes the color of the links black, and the background color is also black. This leads to the links being invisible.
How do I change the color of the links in the RTB?
Phoexo:
Have a look at the following CodeProject article. This fellow provides a way to create arbitrary links in the text that work, while the DetectUrls property is set to false. With a small amount of hacking, you should have full control of the formatting of your links.
Links with arbitrary text in a RichTextBox
http://www.codeproject.com/KB/edit/RichTextBoxLinks.aspx?display=Print
string str = richTextBox1.Text;
Regex re = new Regex("^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+#)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?", RegexOptions.None);
MatchCollection mc = re.Matches(str);
foreach (Match ma in mc)
{
richTextBox1.Select(ma.Index, ma.Length);
richTextBox1.SelectionColor = Color.Red;
}
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/1f757f8c-427e-4042-8976-9ac4fd9caa22
I'm not sure how to change the color of the links, but you can change the way that the RTB handles URLs.
Try setting the DetectUrls property to false.
That way, the link will be the same color as the RTB text, and visible. (Although not clickable).
You could try changing the formatting in the RichText itself. The fonttbl keyword allows you to do text formats.
http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx

Categories