How do I highlight a certain word in a label? - c#

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

Related

WinForms Doughnut and Pie chart labels background element color property

I'm using Doughnut and Pie Charts in WinForms C# desktop application
I change the color for section this way:
chart1.Series[0].Points[i1].Color = Color.Yellow;
as well as for BackColor and BorderlineColor from properties, but I can't find in Series (Collection) Editor property to change white background behind listed values (on right side on image, pointed with red arrow):
I've tried listed color properties, but none of them seems to affect this element:
This thing is called a legend. You can reach it using Chart.Legends property and set its BackColor:
//by index
chart1.Legends[0].BackColor = Color.Black;
//by name through series object
chart1.Legends[chart1.Series[0].Legend].BackColor = Color.Red;

What is the font color of a checkbox' text when it is not enabled

I have a label and a checkbox in C#. I just want to let the labels' font color be the same as checkbox's text when it is not enabled, say a kind of gray. Is there any way to do that?
I am trying to change the font color of the label when the box is no enabled. But I cannot find the correct color.
The SystemColors class contains static properties with the standard colors used by most applications, unless they are using some skinning or any custom colors.
Give it a try with
label1.ForeColor = SystemColors.GrayText;
The description of the property says:
Returns: A System.Drawing.Color that is the color of dimmed text.
The exact color of a disabled label font is: SystemColors.ControlDark.
So you just have to do something like this:
label1.ForeColor = SystemColors.ControlDark;
Here are two labels, one is enabled = false the other is with label1.ForeColor = SystemColors.ControlDark;
You can see they are exactly the same.
If you are using WPF you can get the color of the checkbox text and set it for the label by using
Brush color = _checkBoxName.Foreground;
_labelName.Foreground = color;

Change Label Color in a Chart Control

The question I have seems to be simple but I can't find an answer around the Internet for that and even trying around did not help me.
I just want to change the color of a Legend (from a Series). I know how to change the text color but I need to change the color of that marker.
chart1.Legends["1"].ForeColor = Color.Transparent;
chart1.Legends["1"].BackColor = Color.Transparent;
does not help
Is this possible?
Thank you!
EDIT:
I want to change the blue color to another. Hope this is more clear now.
The markers show the ChartType and the Color of all the Series.
Unfortunately the default LegendItems anre hidden and can't be changed, only expanded.
So your best bet, except avoiding the issue by picking a a different green, (if that is why you want to change the blue) would be to clear or disable the default Legend and create a new one from scratch.
For this you can style all sorts of things including setting MarkerStyles and also Bitmaps you create on the fly..
Here and here are two examples that show you how to do it..
And here is one with a rather extended custom Legend
Instead of creating a new Legend you can also hide the LegendItem for a Series
series1.IsVisibleInLegend = true;
and add a new one but it will be added at the end..
Here is an example of adding a simple LegendItem:
void AddLegendItem(Legend L, Series s, Color mc)
{
LegendItem lit = new LegendItem();
lit.BorderColor = mc;
lit.Color = mc;
lit.SeriesName = s.Name;
lit.Name = s.Name;
L.CustomItems.Add(lit);
}

RichTextBlock paragraphs background color

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.

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