C# + Windows Phone => TextBlock, preserve whitespaces? - 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.

Related

C# wrap text inside a listbox

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));

How to wrap output text from multiline textbox (web control) with a pre-configured width?

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.

How can I insert a newline into a TextBlock without XAML?

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");

Limit number of characters in Label

I am using label in my winform . It displays the value which I enter in another textbox. My problem is label does not display whole characters I enter in textbox. Label's size is width=160 and height=19. So it truncates the last value. For testing purpose when I enter "W" in caps in textbox ;label can display maximum 13 "W"s. So I am trimming the
charater's by using labelname.substring(0,10); and for next three characters I am appending 3 dots(...)
But this solution is not desirable to my senior. He is telling me that if I enter all small letters "l" in textbox then though label has space to display more than 13 characters it will display only 13 characters(including dots).
Does anybody has solution on that ?? I also cannot increase width or height of label to accomodate more characters.
Well, you could set the AutoEllipsis property to true and don't worry about the length of the text.
Edited to Add: as per comments
If you're using RadLabel from Telerik then you need to dig a little more:
RadLabel.LabelElement.LabelText.AutoEllipsis
Nothing that 5 minutes looking through the documentation doens't solve
Set the AutoEllipsis property of your Label control to true. You can do this either in the designer, or through code:
myLabel.AutoEllipsis = true;
That will cause the ellipsis character (...) to be automatically appended to the text if it overflows the space available in the label. You won't have to manage this yourself in code at all.
This property is available as far back as .NET 3.0.
If I understand your question correctly, you can use Textbox.MaxLength property which only allows the user to enter the maximum number of characters you set the value to.
You can set the label's AutoEllipsis property to true and let it figure this out for itself.
Gets or sets a value indicating
whether the ellipsis character (...)
appears at the right edge of the
Label, denoting that the Label text
extends beyond the specified length of
the Label.

Copying formatted text into clipboard

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.

Categories