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");
Related
I want to display a list of words in a listbox, wrapped them together. Below is an example what I want to do. I was able to add words to listbox with a comma and in one line. Can you please help me to wrap this text.
For comma separation I used,
ListBox.Items.Add(string.Join(",", myList));
Expected output-
Below is my output
I do not think its possible to print multiple text lines per ListBox item with the standard ListBox. Try using a TextBox instead, with Multiline = true
this.textBox1.Text = string.Join(",", UniqueWord(myList));
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 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.
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?
I have strange problem with coping text into clipboard. I want to copy text from textbox with additional formatting. In order to do that I intercept KeyDown event on textbox and I check if keys CTRL and C are pressed. Then I do sth like that
string extraFormatedText = formatText(txtBox.Text);
Clipboard.SetText(extraFormatedText, TextDataFormat.Text);
Function formatText adds couple of empty additional lines in text. However if I paste this text into notepad there is no additional formatting.
Why is that ??
If I call function Clipboard.GetText() I can clearly see that in returned string there are extra characters ( \n \r).
\r represents a carriage return and \n a newline (linefeed) character.
The method formatText must be adding these to the end of the string.
Also, the program Notepad has no formatting functionality such as bold or italic, it simply displays everything in the chosen font, size and style.
If you're adding just \n to insert new lines Notepad will not understand that as it expects \r\n as a line break. Try pasting your text into Notepad++ and you'll see that line breaks are actually there.
It's safer to use Environment.NewLine, which on Windows will give you exactly \r\n.