Save Richtextbox to .txt but same format vb.net2010 - vb.net

when i click the button save, the output of richtextbox1 will be save in .txt,, but my problem is not all the format in richtextbox1 is not desame in .txt that i save. how to fix this?
Private Sub ADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ADD.Click
RichTextBox1.AppendText(OutputTstm.Text & " " & vbNewLine)
RichTextBox1.AppendText("*********************" + vbNewLine)
End Sub
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim isave As New SaveFileDialog
isave.Filter = "txt files (.txt) |*.txt"
isave.FilterIndex = 2
isave.RestoreDirectory = False
If isave.ShowDialog = DialogResult.OK Then
IO.File.WriteAllText(isave.FileName, RichTextBox1.Text)
End If
End Sub
![Richtextbox1 to Notepad ][1]ck.imgur.com/Lw6gz.png

The Text of the RichTextBox is just like the Text of a regular TextBox. It is the Rtf property that contains all the markup and thus all the formatting.
The thing is, you don't need to use either property to save the contents to a file. The RichTextBox has a SaveFile method that lets you save either plain text or Rich Text Format (RTF).
There is a corresponding LoadFile method to read a file into the control.
Also, if you're saving RTFrather than plain text, you should be using the ".rtf" extension rather than ".txt".

There is a function in the RichTextBox Class, that allows saving a file without needing to use a StreamReader.
It's RichTextBox.SaveFile().
There are arguments required by this function, and the ones you need are RichTextBox.SaveFile(FileName As String, FileType As RichTextBoxStreamType)
With those arguments, the code you need is RichTextBox1.SaveFile(isave.FileName, RichTextBoxStreamType.PlainText)

Related

Visual Basic Form. How to let users save text file where they want

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim CurrentDir As String = Environment.CurrentDirectory
Dim OutputFile2 As String = IO.Path.Combine(CurrentDir, "input.txt")
IO.File.WriteAllLines(OutputFile2, Result1.Lines)
End Sub
Right now, I have coding that saves a text file in the current directory. However, I want to have a browse button for users so that they can pick where this text file is saved. How do I proceed this?
I was trying it by my self and I'm having a trouble with using save file dialog. If you can teach me how to use a save file dialog or anyway to write save browse button, I would very appreciate it!
The documentation for the SaveFileDialog object contains an example.
Here is a tutorial on how to implement SaveFileDialog using Toolbox in Visual Studio like you mentioned. The code sample is in C# but it can be easily converted to VB.
Link: www.dotnetperls.com/savefiledialog
Private Sub button1_Click(sender As Object, e As EventArgs)
' When user clicks button, show the dialog.
saveFileDialog1.ShowDialog()
End Sub
Private Sub saveFileDialog1_FileOk(sender As Object, e As CancelEventArgs)
' Get file name.
Dim name As String = saveFileDialog1.FileName
' Write to the file name selected.
' ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test")
End Sub

How to Make a File Name appear as Form Text

I am creating a text editor in VB.net with tabs and I am done so far. All I need is when the user saves the document or opens a document the name of the document shows up on the tab. I am using a separate tab control. I have it done but it shows the whole directory of the file. The only way to change the text on the tab is to change the text of the form I am using for the tab control to duplicate. So my code for when the user opens a file is:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim alltext As String
filename = OpenFileDialog1.FileName
alltext = File.ReadAllText(filename)
FastColoredTextBox1.Text = alltext
Me.Text = filename
End Sub
But like I said it shows the whole directory of the file. Is there any way to make it just show the file name. The code for when the user saves the file has the same thing.
Try this:
Me.Text = System.IO.Path.GetFileName(filename)

Write and read a .ini file in VB.NET

Let's say I have a Button that, when clicked, will output text ("test text") to a .ini file in directory (C:/configdir) for example. How would I create this .ini file?
Then I have another Button that, when clicked, will show the content ("test text") of the .ini I just created. I am guessing I need to streamwrite the text.
Here's something that will work.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using sw As New System.IO.StreamWriter("c:\myfile.ini", False)
sw.WriteLine("test text")
End Using
Using sr As New System.IO.StreamReader("c:\myfile.ini")
Dim Line As String = sr.ReadLine
Do While Line IsNot Nothing
MsgBox(Line)
Loop
End Using
End Sub

VB.NET - How to Drag a File From Outlook 2010 to My Form?

I'm developing an application to read XML files, get its data and populate some field. SO, I've implemented two ways to get the file: through a command button and through the drag and drop form event. Everything is OK if I use a file stored in a folder in my computer. I can drag the file, the cursor changes its appearance to the copy effect etc. However, when I try doing it dragging a file as an attachment of Outlook (my version is the 2010), I see that the cursor assumes the forbidden effect and the file is not read.
Please, see below the implementation I did, which is working only for files stored in a folder in my computer. I would like to know what more I have to implement to allow drag a file from Outlook.
Thank you in advance.
Private Sub FrmJIG_DragDrop(sender As Object, e As DragEventArgs) _
Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim filestype() As String
filestype = e.Data.GetData(DataFormats.FileDrop)
Dim sReader As New StreamReader(filestype(0))
'get the filename from the file without the path
Dim file_name As String = Path.GetFileName(filestype(0))
'check the extension of the file
If Path.GetExtension(filestype(0)).ToLower() = ".xml" Then
'Read the xml file
For Each path In files
ReadXMLFile(path)
Next
Else
'warning about the file type
MessageBox.Show("Only XML files are supported!", "Warning!", _
MessageBoxButtons.OK, _
MessageBoxIcon.Warning)
End If
End Sub
Private Sub FrmJIG_DragEnter(sender As Object, e As DragEventArgs) _
Handles Me.DragEnter
'change the cursor type to drag and drop type
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Please try with
Private Sub FrmJIG_DragEnter(sender As Object, e As DragEventArgs) _
Handles Me.DragEnter
'change the cursor type to drag and drop type
If e.Data.GetDataPresent(DataFormats.FileDrop) or e.Data.GetDataPresent("FileGroupDescriptor") Then
e.Effect = DragDropEffects.Copy
End If
End Sub

Streamwriter will not save to FileName entered by User with Default var.FileName initiated - VB.NET

I am new here, and relatively new to VB.NET. I have a specific problem with getting my StreamWriter to work properly. One of the requirements of my project is to give the file a default name when the user clicks the Save button, which I have done by setting mysave.Filename = "MyLog.log". I call a new instance of my streamwriter trying to save to the filename specified by the user (mySave.Filename again), but every time, it saves to the default MyLog.log file. I have pasted my code below.
If someone could tell me how I can make sure the data is being saved to the value entered by the user for File name, that would be greatly beneficial. Also, I apologize for the code format, its not perfectly readable, but I'm trying to learn how to use the 4 space indents to my advantage!!
Thanks!
Imports System.IO
Public Class Form1
Dim Initial As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug" 'give variable for full path without log file
Dim Fullpath As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug\MyLog.log" 'give one path for full path
Dim filewriter As New StreamWriter(Fullpath, True)
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit() ' quit application
filewriter.Close()
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim mySave As New SaveFileDialog
mySave.Filter = "LOG File (*.log)|*.log" 'set filter to .log
mySave.DefaultExt = "log" 'set default extension
mySave.InitialDirectory = Initial 'set default directory
mySave.FileName = "MyLog.log"
mySave.OverwritePrompt = True ' make sure to ask user if they want to over write
If mySave.ShowDialog() = DialogResult.OK Then
Dim filewriter As New StreamWriter(mySave.FileName, True)
filewriter.Close()
'filewriter = New StreamWriter(mySave.FileName, True)
'filewriter.Close() 'close filewriter to allow access to write directory
'Dim Stream As New StreamWriter(mySave.FileName, True) 'save file to path chosen by user in SaveDialog
'Stream.Close() ' Close stream to allow access to directory
' filewriter.Close()
Else
'dialog cancelled - no action
End If
filewriter.Close()
filewriter = New StreamWriter(Fullpath, True) 're initiate filewriter to be used in successive iterations through the program without error
End Sub
Private Sub SavingsDepositBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsDepositBtn.Click
'write to file for SavDep
filewriter.WriteLine("SavDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub SavingsWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsWithdrawBtn.Click
'write to file for SavWith
filewriter.WriteLine("SavWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckDepsotBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckDepsotBtn.Click
'write to file for CheckDep
filewriter.WriteLine("CheckDep")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
Private Sub CheckWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckWithdrawBtn.Click
'write to file for CheckWith
filewriter.WriteLine("CheckWith")
filewriter.WriteLine(AccountBox.Text)
filewriter.WriteLine(AmountBox.Text)
End Sub
End Class
the general way you are using the filewriter won't work, and there are some other serious issues like that there are two 'FileWriter' variables with overlapping scope.
for your save routine, you can't just re-instantiate your filewriter to point it to a different path. that will destroy all the data that had been in the underlying stream, so you are just saving nothing.
try caching the data in memory while the program is running, and don't create the stream until you are ready to write all the data to it, and it to a file, all in one go. your current approach makes it very hard to prevent leaking handles to the log files, so if the app crashes the user will have to reboot to release the write lock before they can run it again. look into the 'using' construct and 'Try... Finally'