My program reads a DDS image file and stores it as a byte array. I want to be able to show the users the raw data in a TextBox form so at first I convert the byte array to a string using the following code:
string data = System.Text.Encoding.ASCII.GetString(bytes);
I then set the TextBox text:
textBox.Text = data;
The problem I am having is the text box is not showing all the data. Here is a screenshot of how it looks:
As you can see only the first few characters are displayed. I am assuming this is because the string contains a null terminator which the TextBox interprets as the end of the string. Here is a copy paste of the first 50 or so characters in the string which I copied directly from the debugger watch window:
DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.
What I want to be displayed is similar to what you see if you edit the raw DDS file with a text editor such as Notepadd++.
Opening the DDS file in Notepad++ produces the following:
My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?
The simplest solution is to use this:
textbox.Text = data.Replace("\0", #"\0");
This will force the textbox to actually show a backslash followed by a zero where the nulls would be. Alternatively, you could replace the nulls with some other character or string.
Related
I am doing some binary decoding of a DataFormat, and is trying to display the text in a RichTextBox in C#.
When looking at the variable in Visual studio the debugger shows this value \u0005\0\0\0\0\0\0\0\0\0?A\b\0building???=\0\0\0\0\0\0\0\0\0\0\0\0\u0003\0\0\0" but my TextBox only shows ``
How could I show an invalid string like this?
Try to change RichTextBoxStreamType.RichText to RichTextBoxStreamType.UnicodePlainText to avoid the Invalid File format.
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 am trying to inset formatted footnotes into an open word document using a WinForms application.
While I am able to use Interop.Word to set plain text footnotes and so long as I use plain text it works fine. However, I also want the user to be able to paste rich text formatted text from a rich text box into the footnote. This never works and always shows the rich text codes.
I know that footnotes can take formatting because if I put the rich text int a clipboard and paste it into a footnote the formatting is preserved.
I have even tried putting the rich text into the clipboard and then setting the string (s) to the clipboard contents using "s = Clipboard.GetText(TextDataFormat.Rtf);" It seems as if this should be exactly what I am pasting, but if I paste into the footnote it works. If the program sets it using the code below it does not work.
I appreciate any help.
application = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
application.ActiveDocument.Footnotes.Add(application.Selection.Range, "", s);
Cindy's answer was helpful. I fixed the problem by doing the following:
Saving the current selected position in the document and current clipboard contents.
Inserting a blank footnote.
Selecting the footnote.
Putting the string into the clipboard as rich text format.
Sending/pasting the information from the clipboard to Word.
Restoring the clipboard and original selected position in the document.
I would like to display mixed western & arabic data read from a file into a text box.
An example of the data is I want to display is:
"You have", "يوجد لديك"
I read the data in as follows:
String tsIn = File.ReadAllText(tbxTSFileName.Text, Encoding.UTF8);
tbxBefore.Text = tsIn;
I cannot see any properties that let me set the textbox to "mixed" encoding.
When I open the file in Notepad++ I select Encoding>Character sets>Arabic>ISO 8859-6.
To make matters even more complicated, I would want to use the same textbox for other languages too, so the solution needs to work in all/most cases.
You need to show us the assignment to the textbox - for instance, is this a WinForms textbox, a WPF textbox or an HTML textbox.
Strings in C# are unicode - so can definitely hold Arabic.
You are reading UTF8 format which supports Arabic so I cannot tell what your actual problem is. Your tsIn could easily contain Arabic - is your problem that you cannot see Arabic in your WinForms TextBox ? This link has a lot of detail on displaying arabic on WinForms
Winforms Arabic Input text box
Am writing data in .rtf Format from a RichTextBox using (Text is color coded )
RichTextBox .SaveFile(path);
There is a 'Clear Text' button on the GUI on click of which clears the RichTextBox .
The problem arises when new data is printed on the RichTextBox instead of appending the data the RichTextBox .SaveFile(path) method clears out the previous data and contains only the newly added data.
How can i append the data? StreamWriter is wrtitng the data in plain text i need it in .RTF.
Can anybody help me on this?
You can save the previous data in a variable, set the richTextBox's data to previous+new and then call the SaveFile method.
Seems like saving from the rich text box overwrites the file. You can have a look at this or this for more info.
Some of the solutions suggested are saving to a different file, or reading, concatenating, and saving.
There is no option to append text to RTF file as you have already read from the comments.
What you can do, however, is use 2 RichEdit controls, one that reads the stream in using EM_STREAMIN message (this will preserve the char format) and concatenate the new data to it. For the preservation of the char format of the new data from the other RichEdit control (colors,fonts etc), you have to use EM_GETCHARFORMAT message on the selection of the new data you want to concatenate. And then, you need to use EM_SETCHARFORMAT message with SCF_SELECTION OR SCF_WORD to set the char format on the RichEdit control that will hold all data together. After that, use EM_REPLACESEL message to concatenate all the data together. Finally, use EM_STREAMOUT message to save all of the stream at one go.