How to 'align' text in RichTextBox C#? - 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.

Related

C# How do you set richtextbox text to default?

When some one puts a highlighted sentence to my richtextbox.
For example,
I want to make them type default text if they want to. Let's say if user clicks a button and want it to make default text. How do I do that?
I have tried
Font arialFont = new Font("Arial", 10, FontStyle.Regular);
richTextBox1.SelectAll();
richTextBox1.SelectionFont = arialFont;
richTextBox1.BackColor = Color.White; // i also tried Color.Transparent but it only return error message "Control does not support transparent background colors"
// RichTextBox's property says that Appearance - BackColor - Window, but it didn't even exist on Color.~
How can I set text all in default state in RichTextBox?
I'm sorry for unclear question.
when a user copies a text that is hightlighted text like above picture. And if they keep type anything, the text is highlighted too. But I don't want that to happen.
So I want to make a button to clear all text and make them to type a default text(no highlight, bold, italic, etc...)
I hope I explained my problem well this time. Sorry for unclear question.
Thank you LarsTech, sorry for my poor English. But I kind of solved the problem by doing richTextBox1.SelectionBackColor = Color.Transparent;
I didn't know about Selection has a lot of different function. Probably from next time, I need to look for more functions that's already in it.
Thank you for the hint and have a nice day!

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.

Adjust label height to height of string

So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!
Thanks in advance.
*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)
Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by #LarsTech in comments)
AutoSize defaults to true - so the default behaviour of the textbox should resize to any font size/content that is in use.
The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property.
You could try it first.
here's an example code you can use. It uses the AutoSize property.
If you label is called Label1 you can change it like this:
Label1.AutoSize = true;
Label1.Text = "The text in this label is longer than the set size.";
And it will automatically change it.

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

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