RTF code saved when I save a txt file - vb.net

I'm working on a VB.NET project.
I have a richtextbox and a button to save what I write.
But when I open the file I see this:
I want to show just the text "ilyasscj isjdivs", not all the RTF code.
This is my code:
Private Sub EnsregistrerSousToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EnsregistrerSousToolStripMenuItem.Click
SaveFileDialog1.InitialDirectory = "c:\"
SaveFileDialog1.Filter = "Texte|*.txt|RTF|*.rtf|Tous|*.*"
SaveFileDialog1.Title = "enregistrer un fichier"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
fich = SaveFileDialog1.FileName
RichTextBox1.SaveFile(fich)
End If
End Sub
Private Sub EnregistrerToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EnregistrerToolStripMenuItem.Click
If fich = "" Then
SaveFileDialog1.InitialDirectory = "c:\"
SaveFileDialog1.Filter = "Texte|*.txt|RTF|*.rtf|Tous|*.*"
SaveFileDialog1.Title = "enregistrer un fichier"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
fich = SaveFileDialog1.FileName
Try
RichTextBox1.SaveFile(fich)
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End If
Else
Try
RichTextBox1.SaveFile(fich)
Catch ex2 As Exception
MsgBox(ex2.Message)
End Try
End If
End Sub
I will appreciate any help.

The RichTextBox.SaveFile(String) pre-defined behaviour is described in the documentation:
Saves the contents of the RichTextBox to a rich text format (RTF)
file.
If you define more format options in the SaveFileDialog, you also need to verify what format the User chose and instruct the SaveFile method to use that format when streaming the output to disk.
As an example, use a Select Case switch to select the RichTextBoxStreamType corresponding to the User choice (see the documentation for the meaning of other possible fomat options):
Dim sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "Texte (*.txt)|*.txt|RTF (*.rtf)|*.rtf|Tous (*.*)|*.*"
sfd.Title = "Enregistrer un fichier"
sfd.DefaultExt = "txt"
If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Filter As Integer = sfd.FilterIndex
Dim FileFormat As RichTextBoxStreamType
Select Case Filter
Case 1
FileFormat = RichTextBoxStreamType.PlainText
Case 2
FileFormat = RichTextBoxStreamType.RichText
Case Else
FileFormat = RichTextBoxStreamType.UnicodePlainText
End Select
Dim FileName As String = sfd.FileName
RichTextBox1.SaveFile(FileName, FileFormat)
End If

Related

How can I get my text file data to display in listbox?

I have an issue that I would like some help with.
I am trying to open a file dialog, select text file, and then display text in listbox.
I have the following code. It opens dialog, but won't display text in listbox.
Any suggestions?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportKeywordList.Click
Dim oReader As StreamReader
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
oReader = New StreamReader(OpenFileDialog1.FileName, True)
ListBox1.Text = oReader.ReadToEnd()
End If
End Sub
Listbox display text through the Items collection not through the Text property. In a ListBox the Text property represent the text of the currently selected item
An example could be written in this way
....
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Using oReader = New StreamReader(OpenFileDialog1.FileName, True)
While oReader.Peek <> -1
ListBox1.Items.Add(oReader.ReadLine())
End While
End Using
End If
....

how to save a file in VB.NET?

i'm trying to use the savefiledialog tool, but it don't create the file to the selected destination...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
saveFileDialog1.Title = "Save a Text File"
If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK & saveFileDialog1.FileName.Length > 0 Then
RichTextBox2.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
End Sub
The binary logical operator AND in VB.NET is expressed with the keyword AND not using & (string concatenating operator)
If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK And
saveFileDialog1.FileName.Length > 0 Then
....
If you set Option Strict On this problem will be signaled at compile time
(By the way, there is no need to test for the filename length. The dialog doesn't close if you don't supply a filename)

How do I get the file name only, assign it to a variable and use later?

I have the following code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
Dim saveFileName As String = ""
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
saveFileDialog1.FileName = saveFileName
Using sw As StreamWriter = New StreamWriter(myStream)
' Add some text to the file.
sw.WriteLine(DateTime.Now + " - " + saveFileName) ' Date and File title header
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Using
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
End Sub
The variable is saveFileName, however when I use the saveFileDialog1.FileName as it, it is empty or provides the full path to the file, how do I only get the name? (such as test.txt)
Use Path.GetFileName
saveFileName = Path.GetFileName(saveFileDialog1.FileName)
DIM fileName = IO.Path.GetFileName(SavefileDialog.FileName)

Save Data to text tile using SaveFileDialog?

I have already viewed the MSDN Example but I am still having problems.
I created a super-simple program to multiply two numbers, and display the output in the textbox. Now I need to be able to read that text box value and put the value in a text file, bringing up the save to file dialog when the "Save To File" button is clicked.
Private Sub MutiplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MutiplyBtn.Click
Dim FirstNum As Double = Num1.Text
Dim SecondNum As Double = Num2.Text
Dim Answer2 As Double = FirstNum * SecondNum
Answerbox.Text = Answer2
End Sub
Private Sub SaveResultToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultToFile.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
System.IO.File.WriteAllText(Answerbox.Text)
myStream.Close()
End If
End If
End Sub
Currently, Visual Studio is giving me an error: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.
WriteAllText static method requires the name of the file where the data should be written to.
You could use directly the name selected in the saveFileDialog1
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(saveFiledialog1.FileName, Answerbox.Text)
End If
instead if you really want to use the stream opened by OpenFile() method your code should be
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sw As StreamWriter = new StreamWriter(saveFileDialog1.OpenFile())
If (sw IsNot Nothing) Then
sw.WriteLine(Answerbox.Text)
sw.Close()
End If
End If
The code is an example, you need to add a bit of error handling
Hi I tried above method but I succeed in this way....
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
Then
My.Computer.FileSystem.WriteAllText _
(SaveFileDialog1.FileName, RichTextBox1.Text, True)
End If
End Sub

trying to output more than one filenam into textbox vb.net

Apologies if this is really simple but I'm fairly new to programming. I've created a program that uses an open dialog box and outputs the names of the file to a textbox.
Where I'm having issues is trying to get the textbox to display more than one line as all it seems to be doing is writing one line in the textbox.
The code I'm using is below, could someone please advise what I need to change so that I can get this to work.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFileName As String
OpenFD.Multiselect = True
OpenFD.InitialDirectory = "\\server\filename\"
OpenFD.Title = "Open a Text File"
OpenFD.Filter = "Text Files(.txt)|*.txt"
Dim DidWork As Integer = OpenFD.ShowDialog()
strFileName = OpenFD.FileName
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileName
TextBox1.Text = strFileName += 1
End If
End Sub
I've managed to get everything else to work correctly but it's just this one thing.
Dim strFileName() As String
'...
Dim DidWork As Integer = OpenFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileNames
TextBox1.Multiline = True
TextBox1.Text = ""
For Each sFile as String in strFileName
TextBox1.Text &= sFile & System.Enviroment.NewLine()
Next
End If
Set TextBox.Multiline property to True