how to save text from richtext box to text document in vb.net - vb.net

I'm taking value from richtextbox and i want to save it in new text document
I Want to save text of rich text box in text document as i write in rich text box but it stores like shown in image
My Code
Using Sw As StreamWriter = File.CreateText(path)
Dim number As String = RichTextBox1.Text
Sw.WriteLine(number)
End Using
Please help

The problem here is that both the TextBox and the RichTextBox control uses LF (Line Feed) as their newline format, but Windows's newline format is actually CR + LF (Carriage Return + Line Feed), which is what Notepad expects it to be.
The line breaks are there, Notepad just doesn't render them.
To fix this you can either replace all LFs with Environment.NewLine (which adapts to the current system) before saving:
Dim number As String = RichTextBox1.Text.Replace(vbLf, Environment.NewLine)
Sw.Write(Number)
...or you can save it line-by-line instead using the StreamWriter.WriteLine() method (which uses CR + LF for line breaks):
Using Sw As StreamWriter = File.CreateText(path)
For Each Line As String In RichTextBox1.Lines
Sw.WriteLine(Line)
Next
End Using
Read more:
Newline - Wikipedia (Line Feeds)
Carriage return - Wikipedia

After each Write, perform .Write(Environment.NewLine)

Related

Reading a .dat file

I have a file I am trying to read and disply results into a text box. The file has no extension, but it is a 'data' file per the website (www.checkfiletype.com).
Here is a screen shot of how the file looks in a online reader, it looks like hex?
I have tried a stream reader, and gives nothing in results. Last method I tried was a BinaryReader, that I have never used before. The results from this is a "0" into the text box. Given that I have never used the BinaryReader function, I am sure I did something wrong with it.
Using reader As New BinaryReader(File.Open("C:\Users\jefhill\Desktop\CMOSDATA", FileMode.Open))
Dim pos As Integer = 0
Dim length As Integer = reader.BaseStream.Length
While pos < length
' Read the integer.
Dim value As Integer = reader.ReadInt32()
' Write to screen.
TextBox1.Text = value
' Add length of integer in bytes to position.
pos += 4
End While
End Using
Any help would be greatly appreciated.
EDIT
I tried using a basic StreamReader. With this, nothing happens, as in no errors, just puts nothing(blank) into the textbox.
Dim file As String = "C:\Users\jefhill\Desktop\CMOSDATA"
Dim reader As New System.IO.StreamReader(file)
TextBox1.Text = reader.ReadToEnd
reader.Close()
The file is not a text file, and cannot be directly displayed in a TextBox. You will need to find the format of the file and convert it to text in order to display it in a TextBox.

replace text in richtext box quickly

i'm trying to load a text file to richtextbox using this codes
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("path")
RichTextBox1.LoadFile("path", RichTextBoxStreamType.PlainText)
but both of them take time to load the file ,, the file size is around 400-1MB
so how to load it more quickly?
and with my code after loading the textfile i use this code
RichTextBox1.Text = Replace(RichTextBox1.Text, "text", "othertext")
but the problem is this take alot of time !!
How to do it quickly and save time :) , thanks!
You can cut the time almost in half by using an ordinary string variable instead of RichTextBox1.Text in the Replace function:
s = My.Computer.FileSystem.ReadAllText("path")
s = s.Replace("text", "othertext")
RichTextBox1.Text = s
You can combine these into one or two statements, but separating them allows you time each operation. The time-consuming part is accessing the RichTextBox control.
You could try reading it line-by-line:
Using Reader As New IO.StreamReader("<File Path>")
Do Until Reader.EndOfStream
Dim Line As String = Reader.ReadLine()
Line = Replace(Line, "text", "othertext")
RichTextBox1.AppendText(Line & Environment.NewLine)
Loop
End Using

Display “&” from text file

I am trying to display some text from a .txt file in a label, but the text has an ampersand in it which will not display. I am getting the text by reading singles lines using a System.IO.StreamReader. I tried using a backslash as an escape character, but all that did was display a backslash in the label.
Use another & as an escape character.
&&
Dim sr As New StreamReader("C:\yada.txt")
Dim line As String = sr.ReadLine
Label1.Text = line.Replace("&", "&&")

rich textbox combines all lines into one when saved as txt file?

I have a rich textbox in vb.net, which contains several lines of text however when I try and save the text to a .txt file all the lines are combined into one???
How can I overcome this?
what i did try was:
Dim MYLINES As Object
For Each MYLINES In RichTextBox1.Text
objWriter.WriteLine(MYLINES.ToString & Environment.NewLine)
Next
objWriter.Close()
however this simply placed every single character on a difrent line...
Save it as a .RTF file (instead of .TXT), wich can process the line breaks directly. Thats an option.
Or you can wirte the lines individually:
Dim sw As New System.IO.StreamWriter(sFileName)
For Each sLine as String in TextBox1.Lines
sw.WriteLine(sLine)
Next
sw.Close()
The method WriteLine already adds a line break at the end. It more or less the same as:
.Write(sLine & Enviroment.NewLine())

How to replace CRLF with a space?

How can I parse out undesireable characters from a collection of data?
I am working with existing VB.NET code for a Windows Application that uses StreamWriter and Serializer to output an XML document of transaction data. Code below.
Private TransactionFile As ProjectSchema.TransactionFile
Dim Serializer As New Xml.Serialization.XmlSerializer(GetType (ProjectSchema.TransactionFile))
Dim Writer As TextWriter
Dim FilePath As String
Writer = New StreamWriter(FilePath)
Serializer.Serialize(Writer, TransactionFile)
Writer.Close()
The XML document is being uploaded to another application that does not accept "crlf".
The "TransactionFile" is a collection of data in a Class named ProjectSchema.TransactionFile. It contains various data types.
There are 5 functions to create nodes that contribute to the creation of a Master Transaction file named TransactionFile
I need to find CRLF characters in the collection of data and replace the CRLF characters with a space.
I am able to replace illegal characters at the field level with:
.Name = Regex.Replace((Mid(CustomerName.Name, 1, 30)), "[^A-Za-z0-9\-/]", " ")
But I need to scrub the entire collection of data.
If I try:
TransactionFile = Regex.Replace(TransactionFile, "[^A-Za-z0-9\-/]", " ")
Because TransactionFile cannot be converted to String I get a "Conversion from type 'Transaction' to type 'String' is not valid" message.
Bottom Line = How can I replace CRLF with a space when it shows up in TransactionFile data?
Don't do it this way. Create the serializer with XmlWriter.Create(). Which has an overload that accepts an XmlWriterSettings object. Which has lots of options to format the generated XML. Like NewLineChars, it lets you set the characters to use for a line end.
As Hans says, mess around with the XmlWriterSettings.
The next best choice is to write the file, then read the file into an xml object and process it element by element. This would let you remove crlf from within individual elements, but leave the ones between elements alone, for example.
Another possibility - rather than write directly to the file, you can make an intermediate string, and do a replace in that:
Dim ms As New MemoryStream
Serializer.Serialize(ms, TransactionFile)
ms.Flush()
ms.Position = 0
Dim sr As New StreamReader(ms)
Dim xmlString As String = sr.ReadToEnd
sr.Close() ' also closes underlying memorystream
Then you could do your regex replace on the xmlString before writing it to a file. This should get all the crlf pairs, both within elements and between.