Appending data to saved file from RichTextBox - c#

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.

Related

Insert a formatted footnote using Microsoft.Interop.Office.Word

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.

Tab Delimited Text Copy Paste To Excel

I am generating a block of text via C# Stringbuilder, with appropriately tab-delimited text (new lines, "\t", etc.) and displaying the text within a tag in my web application. If I copy/paste this to Excel, all the text pastes into the first column on the spreadsheet.
If I copy the outerHTML of the block (Chrome) or copy/paste into Notepad++ and THEN paste to Excel, all the data pastes neatly into individual cells - which is the desired outcome of this effort. Can anyone tell me what I need to do in order to be able to copy the text from my application and paste it into Excel, so that the text pastes into individual cells (ie, keeps the tab-delimited format)?
Unfortunately, it appears an extra step is required.
The default behaviour of paste in Excel (& many other applications) is to use the original format of the contents of the clipboard.
Copying from a web application/HTML page will result in the clipboard contents being flagged as HTML. The clipboard contents will have the tab character, but as HTML renders tabs to whitespace (compacting to a single whitespace if there are multiple tabs) - just using control-V will convert the tabs to spaces & the contents will appear in a single cell when pasting into Excel..
You will need to use the "Paste Special" option & select "Unicode text" to retain the tabs. There is no way to set this by default : https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_windows8-mso_365hp/how-to-change-default-paste-behavior-with-ctrl-v/f58da075-8fd5-4c80-a64b-5e71ec8ad38b?auth=1
Copying the outerHTML of Chrome sets the clipboard format to text in the first place, pasting to Notepad++ does paste as text rather than HTML then recopying sets the format to text - which is why these methods work.
I was able to paste to Excel and keep the format by changing the element my text wrote to from a div to textarea. Copy/paste of tsv from textarea to a tab-delimited Excel spreadsheet keeps the data formatted in tabular format when pasted. Thanks all for the help.
Try this ,
Before pasting data set excel's text to column delimiters to space and try to paste your values again.
Old topic but I had this. Need to copy something each day from a programme which outputs to google chrome browser. Whenever it was copied and pasted into excel it was going into 1 cell. Yet if I control all and paste it was ok.
I found you can copy only the lines you want and it will behave as wanted - if you don't drag the cursor all the way down off the page. ie if you highlight down to the last piece of information in the bottom right of screen more carefully, when you past to excel it pastes it as tab separated. Whereas if you drag it right down it pastes it all in 1 cell. Might help someone in the future who has this.
You can use 'text to columns' in Excel to format the data in the way you want it to.
After pasting the data into excel, select the column with your data.
Go to Data - click 'Text to columns'
Choose delimited and hit next
Choose Tab and hit finish.
If you don't want to use this extra step, the following works for Excel 2013:
Go to Home, Paste options and click 'Use text import wizard'
Choose delimited and hit next
Choose Tab and hit finish.

Import Txt file to access database

i have a txt file with format
10000025|246|31032017|00|0|0.00|0.00|0.00|0.00|0|0.00|0.00|0.00|0.00|0.00|0.00|0.00|R
10000025|246|31032017|01|0|0.00|0.00|0.00|0.00|0|0.00|0.00|0.00|0.00|0.00|0.00|0.00|R
10000025|246|31032017|02|0|0.00|0.00|0.00|0.00|0|0.00|0.00|0.00|0.00|0.00|0.00|0.00|R
10000025|246|31032017|03|0|0.00|0.00|0.00|0.00|0|0.00|0.00|0.00|0.00|0.00|0.00|0.00|R
i want import to access database using C# code
If someone has a clue about how to perform something like this, i would be grateful !
I dont think there is any need to use c# sort of thing in this scenario. You can accomplish this task by using an excel sheet. Copy the whole txt file and paste it into the excel sheet, once done Select the header of the cell in which you copied the text (Lets say 'A'). Now in the top menu navigate to Data Tab and select "Text to Columns". A dialogue will appear where you can select Delimited (which is default selection) and click next there you will find the delimiters section where you can specify your delimiter like comma, semi colon, etc. In your case the separator is pipe character '|' so you can specify the pipe character in a text box given in the delimiter list.
Once done click next and finish you will get the data in a column and row form exactly what you were looking for now you can import the data to sql, access or to what ever you want take a look at this Link to know how to import data from excel to access
I am attaching following screen shots
Picture of text data being copied in excelwith header selected
Screen shot of delimiter screen where you will specify the pipe character

C# / WPF : Rendering HTML from a string onto a WPF control

I'm writing a program to display the contents of an Excel worksheet using a Grid. To that end, I'm retrieving the worksheet as one big string full of XML using Office Interop, like so :
// Get the cells Range from Excel:
Range rng = sheet.get_Range(Cell(row1, col1), Cell(row2, col2));
// Get the XML representing that Range:
return rng.get_Value(XlRangeValueDataType.xlRangeValueXMLSpreadsheet);
I could create a FlowDocument from this XML string, and if I do assign it to a control on my GUI then that control will render the XML so that it looks exactly like what Excel shows. But I will have all the cells in the Range rendered onto a single control, which is not what I want : I want to be able to render any given cell onto any given control.
I couldn't find a fast and elegant way to extract the XML for individual cells via Interop : it just takes forever and a week. So I spent a whole day writing a class that parses the big XML string into an array of XML fragments each representing a cell. The strings in that array look like this :
<Cell><ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40"><Font html:Color="#000000">Some random text with </Font><B><Font html:Color="#000000">some bold characters</Font></B><Font html:Color="#000000">, and so on and so forth</Font></ss:Data></Cell>
This is actually HTML : from Visual Studio's debugger I can use the HTML viewer and it'll show me this :
Some random text with some bold characters, and so on and so forth
And by the way, this line was rendered correctly by the Stack Overflow website as soon as I pasted it in.
So I felt pretty confident that I could now create, programmatically, FlowDocument objects for each Excel cell, assign them to my GUI controls, and I'd get nice rendering of my Excel cells in any layout I care to program.
Except this doesn't work. When I put the FlowDocument into a control, the control just displays the HTML source code, it does not render it.
Here's how I create the FlowDocument :
FlowDocument tmp = new FlowDocument();
TextRange tr = new TextRange(tmp.ContentStart, tmp.ContentEnd);
byte[] byteArray = Encoding.ASCII.GetBytes(string_full_of_html);
MemoryStream stream = new MemoryStream(byteArray);
tr.Load(stream, DataFormats.Rtf);
(to explain : I allocate a FlowDocument, then create a TextRange through which I'll load the HTML, then turn my string full of HTML into a stream, then feed that stream to the TextRange)
I've tried different "DataFormats" : HTML, oddly, doesn't work. It gives me the exception "'HTML Format' data format not supported". RTF works but, like HTML, only displays the string I fed in, it doesn't render it.
Visual Studio can render my XML strings with its "HTML Visualizer", and I understand it is written with WPF. So I know there's a mechanism in there that does what I need, but I can't find it. That's where you come in, brave code Jedis !

Displaying Raw Data From Image File Using TextBox or RichTextBox?

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.

Categories