Okay, I will just leave my code here.
As you can see from that code, there is a button to make text bold, but not the whole text, just next things user is going to write.
For instance, when user types abc, clicks the button, types def: he gets: abc def.
But, when use: richtextbox2.text = richtextbox1.text;, richtextbox2.text value becomes abcdef, instead of abc def.
I want to copy exact text, including bold text.
Thanks.
use the RTF property of the text box rather than the Text property...
richtextbox2.Rtf = richtextbox1.Rtf
Please award to essedbl as he deserves the points but another method which can come in handy with RTF boxes is to use the SelectedText property...
Specifically, set SelectionStart to be SomeRTFControl.Text.Length and SelectionLength to 0.
Then, set the SelectedText property to whatever you want and use the SelectionX properties to format the color, font, size, etc... of the text you're appending.
This doesn't result in visual artifacts/flickering but allows a very high level of control over text you're adding to an RTF programatically and also gives the usual strong-typing advantages.
Hope this helps.
None of this works:
this.rtxtReport.Rtf = "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 Tahoma;}}";
this.rtxtReport.Rtf += "{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}";
this.rtxtReport.Rtf += "{\\header\\pard\\qr\\plain\\f0\\chpgn\\par}";
this.rtxtReport.Rtf += "{\\pard{\\b ";
this.rtxtReport.Text += this.Ln + "> " + "VSTFS Report - " + System.DateTime.Now;
this.rtxtReport.Rtf += " \\b}\\par}";
The only way so far I've been able to bold text is to select it which isn't practical, I'm creating the doc from scratch, you'd have to select the text you're adding, did that, it did bold that text but also everything else added later!! ... the select(start, length) has length on it so a bust.
Anyone actually get C# to bold text by using rtf formatting and not having to select text?
Related
I have a problem with text wrapping in TextBox (System.Web.UI.WebControls).
I have a textbox with columns set to 65, text mode property set to MultiLine and wrap property set to true.
When I gather the message, which the user has typed in with txtMessage.Text as a string, the line breaks "\r\n" are only put in where it happens in the UI, and not after 65 chars.
Is this possible to solve, whiteout writing my own wrapping code?
Ex. code:
In some init method:
txtMessage.Columns = 65;
txtMessage.TextMode = TextMode.MultiLine;
txtMessage.Wrap = true;
... The user types in some text and Submit ....
In an extraction method:
string text = txtMessage.Text;
// ...
// Do something with the text ...
The text, as typed, is:
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
during extraction the string becomes:
"01234567890123456789012345678901234567890123456789012345678901234567890123456789\r\n01234567890123456789"
since the text window only fits 100 chars in one line.
But the column is set to 65, so the result should be:
"01234567890123456789012345678901234567890123456789012345678901234\r\n56789012345678901234567890123456789"
Winforms TextBox control doesn't support the functionality you require natively. But, there are two solutions for you.
Remove all newline and line breaks. Then add line break and new line characters after every 65 (or whatever the length you'd like) characters just before saving the values to database.
Inherit TextBox class and write your own User Control to override the Text property to represent the functionality in point one above. In this way you could reuse this control (But, not sure that's a concern)
Hope this helped.
I have created a WPF TextBlock inside a Label in code (XAML is not possible in my case) as follows:
Label l = new Label();
TextBlock tb = new TextBlock();
l.Content = tb;
I then come to a situation where I need to set the .Text property of TextBlock containing a new line, such as:
tb.Text = "Hello\nWould you please just work?";
I have tried numerous encodings (HTML encoding, ASCII encoding, etc.) of various newline combinations (carriage return, linefeed, carriage return plus linefeed, linefeed plus carriage return, double linefeed, double carriage return, etc... ad nauseum).
Answers should contain absolutely no XAML.
Answers should assume that the original objects were created using C# code and not XAML.
Answers should not refer to binding of WPF properties. No binding is being used. The "Text" property of the "TextBlock" object is being set. The newline must be inserted there.
If this is impossible, please let me know how I can programmatically add newlines by dynamically replacing each newline in an arbitrary input String into a LineBreak object (or whatever is needed to get this working). The input String will have arbitrary (readable text) contents that I am unable to anticipate in advance because the text itself is dynamic (user-defined); the source strings will have the linefeed character (aka LF, aka \n) but I can easily replace that with whatever is needed.
Also, if it is easier to do this with a Label directly rather than a TextBlock in a Label, that is good too -- I can use that. I just need a control with automatic plain text line wrapping.
You have a few choices:
Use the Environment.NewLine property:
TextBlock tb = new TextBlock();
tb.Text = "Hello" + Environment.NewLine + "Would you please just work?";
Or, manually add Runs and LineBreaks to the TextBlock:
TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Hello"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new Run("Would you please just work?"));
Just another small note. I had a similar problem and took the string from a resource file. What I noticed is that .NET delivered the string "\r\n" as "\\r\\n". When I corrected that by replacing the double-backslashes with normal backslashes everything worked as expected.
You can store string values in "Resources.resw", and get them in code directly.
In the VS resource editor itself not possible to add empty lines to string values. To do this, just edit value in any text editor, add empty lines how many you need, and "Copy -> Paste" to Value column of Visual Studio's resource editor.
For example: value of "Farewell" with 2 line breaks: Goodbye\n\nSave changes? - will look like this in text editor (and as it is, need to be copied to VS):
Goodbye
Save changes?
To get string value of the resource in C# code, call 1 of the following lines (UWP or WinUI3), depending on your application.
UWP code:
Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView().GetString("Farewell");
WinUI3 code:
new ResourceLoader().GetString("Farewell");
I've a RichTextBox and I want color text in it. Is there any tag option? I'd like something like this [color:red]nick[/color] some message. Because I need to save it as text and I want on reload have also colored text.
Can I do something like this without writing own method?
You can set color for text in RichTextBox with SelectionColor
And if you want to save your rtf as plain text, then you will have to look at rtf format. Example:
{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line \cf2 This line is red\line \cf1
This line is the default color }
EDIT:
From this example - first of all you have to declare color table \colortbl in fololowing format:
{\colortbl; color1; color2; ... ; colorN;}
And then in the text you will have to enclose text with {\cfN YOUR_TEXT} where N is a number of color from table; you can not specify the boundaries of the block {}, then everything after \ cfN will be one color.
As the name says RichTextBox contains RichText
to change the Rtf text with 'rtf specific-tags' you can set/use the
RichTextBox.RtfProperty
also take a look at RichTextBox.SelectionColor to color text patterns in code
but when you don't want to use rtf, you said
need to save as text.
you could write your own 'markup' there is no built in expect rtf/html?
but rtf is text - at all
Example to use RichTextBix.SelectionColor to Color the text
richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;
colors the start of "Hello" red
and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox
If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. This will give you a hint about how to encode your formatting. Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included.
rtfMain.SaveFile(dlgSave.FileName);
From Reference Save text from rich text box with C#
I am creating an application where I need to loop through a series of text lines that may contain whitespace.
I build up a string by doing my_string += the_line_to_add and update the Text property of the TextBlock with the final string.
Pretty simple actually, however, a line that looks like this:
"a b c"
will end up as follows:
"a b c"
I don't want all of those spaces to be removed though. I want the line to keep the extra spaces and remain unchanged:
"a b c"
The TextBlock is created programmatically and added into a StackPanel. I looked at the different properties but just can't figure it out.
Honestly, I'd approach this problem differently. I wouldn't use whitespaces in a string to layout the text. If you need 3 string in the screen add 3 textboxes and set Margin proprety. This depends on the input text, but if there will be too many whitespaces the text will be out of the screen.
Alternatively, you can use Run to format the text.
I've been having this problem for a few days. Whenever I update a label in a StatusStrip object it formats my text backwards. I send my label something like
toolStripVoltage.Text = batteryVoltage.ToString("F2") + " V";
and the label will display V 2.82.
and when I send it something like
toolStripVoltage.Text = batteryVoltage.ToString("0.00 V");
it will display the same thing. It seems like no matter how I format the string the "V" goes before the numbers. and! it still puts a space in between the unit and the number. And here's the kicker: when I call this same text to appear in a tooltip of another object like this
toolStripVoltage.ToolTipText = toolStripVoltage.Text;
It displays as 2.82 V. Any ideas on how I can make this work for me?
EDIT:
oh wow. I instantly figured this out somehow...the default RightToLeft property is Yes. I don't know why that would be! but the trick was to set that to No. Very strange for that to be the default setting.