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.
Related
I'm adding text to a FlowDocumentReader within a single paragraph like this
FlowDocumentReader fdr = new FlowDocumentReader();
fdr.Document = new FlowDocument();
Paragraph p = new Paragraph();
Run r = new Run("some text");
p.Inlines.Add(r);
fdr.Document.BLocks.Add(p);
NOTE: I am actually combining multiple runs of text into one paragraph.
However, when I display the text in the FlowDocumentReader, it always shows some extra characters after the very last character of the text I inserted. When I try to drag and select the entire text, and copy, it goes beyond the last character. And when I paste the entire selection to Notepad++ it shows there is a CR LF characters at the end.
So if I start with this text
some text
I end up with
some text\r\n
Is there anyway to eliminate these from becoming part of the text in the FlowDocumentReader itself? I actually don't want them to appear in the FlowDocumentReader. I require that the text not contain any additional characters beyond those that were part of the original text I added to the Runs.
The closest issue I can find similar to this is for RichTextBox
Why does RichTextBox implicitly add newline?
I am unable to get a multiline text box to render strings separated by Environment.NewLine as strings on multiple lines.
I am querying a database and receiving back a string that is pipe delimited text (for example 123|456|789).
I want to parse that text and put it into a multi-line text box on a Windows Form application. I am parsing the text and doing a replace | with Environment.NewLine. In the text box the string is rendered on one line this way: 123456789. However, when you copy/paste the text out of the box it is correctly parsed (it will have 123 on line 1, 456 on line 2, 789 on line 3).
I want the text to render in the text box with line breaks.
I have tried parsing the string in different ways (using a StringBuilder). I have tried explicitly setting the textbox to multiline = true.
I am parsing the string this way (where orabddr is my Data Reader from the database):
txtTracking.Text = oradbdr.GetString(11).Replace("|", Environment.NewLine);
I want my multiline text box to reflect where the Environment.NewLine characters are.
Double check your string is actually delimited how you've been told.
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 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 have a string that gets saved as rich text to a SQL field, so it has a lot of symbols in front of it like so...
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par
I'm passing in this to Crystal Reports 11.5 as a parameter field, so the data type is string. I'm then just displaying it straight up by dragging the parameter to the report. The interpretation is set to RTF also in the paragraph tab of the formatting settings for this.
My problem is, it is removing the formatting tags but it is screwing up the text. It is randomly garbling words and inserting characters like the letter 'i' randomly in words, it's also putting some random letters in the background behind other words, making it look like it's been typed over manually if that makes sense, just makes the word look unreadable and bolded like someone typed over it with a typewriter.
I tried setting a new text object and then putting the rich text parameter in that to display, but then it doesn't remove/do any of the rich text formatting tags.
What gives? It can obviously do rich text as it removes the tags, but what is the deal with the random i's inserted into words and a couple of instances of the jumbled text over other words?
Unfortunately, I haven't used 11.5 yet.. but give this a try..
Format Field > Paragraph tab > Text Interpretation > RTF text
If the text is getting overlapped, then you can replace the following control set from your RTF text. It should work properly and not overlap.
data = data.Replace(#"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}} {\colortbl ;\red0\green0\blue255;} \viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par", string.Empty);