I am not getting why this is not working. I have tried to get this to work, TallDude has done a great deal to help me better understand this all. But I am still having trouble to get this to work.
I want to understand how the NotePad FIle>Save works. I am thinking, as TallDude as suggested, that they use StreamReader/Writer. So in that thinking I am strying to get the File>Save Function to work.
Here is the entire code:
Imports
System.IOPublic
Class Form1 Public FilePath As String = "" 'Open Option Private Sub mnuOpen_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuOpen.Click Dim open1 As New OpenFileDialog 'display only text files in the dialogopen1.Filter =
"Text files|*.txt"FilePath = open1.FileName
'Show the dialog and see if the user clicked 'open' or 'cancel'. ' Result 'OK' means Yes/Open/Okay etc. If open1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Use the filename to open the file using (ReadAllText), ' and store all the text in txtEdit.texttxtEdit.Text =
My.Computer.FileSystem.ReadAllText(open1.FileName) End If End Sub ' SaveAs Option Private Sub mnuSaveAs_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuSaveAs.Click Dim save1 As New SaveFileDialog 'display only text files in the dialogsave1.Filter =
"Text files|*.txt" If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Use the filename to save the file using (WriteAllText), ' and store all the text in txtEdit My.Computer.FileSystem.WriteAllText(save1.FileName, txtEdit.Text, False) End If End Sub Private Sub mnuExit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuExit.Click End End Sub Private Sub mnuNew_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuNew.Click Dim intReply As Integer If txtEdit.Modified ThenintReply = MsgBox(
"The File has not been saved. Discard the change ", _MsgBoxStyle.YesNoCancel)
If intReply = MsgBoxResult.Yes ThentxtEdit.Clear()
End If ElsetxtEdit.Clear()
End If End SubHere is the save portion that I am working on. What am I doing or NOT doing. Please help me with the understanding and writing of this code. I really appreciate it.
Private Sub mnuSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuSave.ClickFilePath = save1.FileName
Dim save1 As SaveFileDialogsave1.FileName = Path.GetFileNameWithoutExtension(FilePath)
save1.Filter =
"Txt files (*.txt)|*.txt" If save1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then Dim writerVar As StreamWriterwriterVar =
New StreamWriter(save1.FileName, False) Me.Text = Path.GetFileName(save1.FileName) End If End SubEnd
Class
UGH....Save Function
mobigital
System.Windows.Forms.Textbox.Modified
http://msdn2.microsoft.com/en-us/library/system.windows.forms.textboxbase.modified.aspx
All this is, is a property on the form that can be set and read which you can used to indicate that some changes have actually occured in the textbox.
So when I initially create a file, or open the file I set the property to false - because nothing has chnaged yet, when I make a change the textbox text changed event occurs which then sets the modified property to true.
Then when you come to open again the value is true if it changed from the original or false if no changes have been made to the textbox contents.
GILLT
I didnt see anything in the code specifically setting the txtmodified property.
Highlighted code changes
Public Class Form1
Public FilePath As String = ""
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
'//Save always displays dialog
Dim save1 As New SaveFileDialog
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to save the file using (WriteAllText),
' and store all the text in txtEdit
My.Computer.FileSystem.WriteAllText(save1.FileName, TxtEdit.Text, False)
FilePath = save1.FileName
End If
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
'//Reset Filepath if clear box as new file
Dim intReply As Integer
If TxtEdit.Modified Then
intReply = MsgBox("The File has not been saved. Discard the change ", MsgBoxStyle.YesNoCancel)
If intReply = MsgBoxResult.Yes Then
TxtEdit.Clear()
FilePath = ""
TxtEdit.Modified = False
End If
Else
TxtEdit.Clear()
FilePath = ""
TxtEdit.Modified = False
End If
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'//Set Path to the Filename of the file being opened - Only set if filename is selected
Dim open1 As New OpenFileDialog
'display only text files in the dialog
open1.Filter = "Text files|*.txt"
'Show the dialog and see if the user clicked 'open' or 'cancel'.
' Result 'OK' means Yes/Open/Okay etc.
If open1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to open the file using (ReadAllText),
' and store all the text in txtEdit.text
If TxtEdit.Modified Then
If MsgBox("The file has not been saved. Save ", MsgBoxStyle.YesNoCancel) = MsgBoxResult.Yes Then
My.Computer.FileSystem.WriteAllText(FilePath, TxtEdit.Text, False)
End If
End If
TxtEdit.Text = My.Computer.FileSystem.ReadAllText(open1.FileName)
TxtEdit.Modified = False
FilePath = open1.FileName
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim save1 As New SaveFileDialog
'// If filepath is set then save to this file otherwise prompt for filepath
If FilePath.Length = 0 Then
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to save the file using (WriteAllText),
' and store all the text in txtEdit
My.Computer.FileSystem.WriteAllText(save1.FileName, TxtEdit.Text, False)
FilePath = save1.FileName
End If
End If
If FilePath.Length > 0 Then
My.Computer.FileSystem.WriteAllText(FilePath, TxtEdit.Text, False)
End If
End Sub
Private Sub TxtEdit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtEdit.TextChanged
TxtEdit.Modified = True
End Sub
End Class
GaneshSharma
Does you code contain the module level variable
Public FilePath As String = ""
The code above shows it in blue
Also put a breakpoint on the line and look at the values. Is FilePath set.
You can put something in here that if filepath.length = 0 then you going to need to prompt for a save path. Otherwise it has no filename to use to same. The filename will be blank
orenga
Method names have changed as I'm using the default menustrip item names. This works as detailed.
The bold items are the key lines which need to be in place.
What are you missing
in the open youve got the setting of the filename prior to the dialog, meaning its not being set by the user selections in the OpenFiledialog.
Your not clearing the filepath variable if you do a new and clear the textbox.
Public Class Form1
Public FilePath As String = ""
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
'//Save always displays dialog
Dim save1 As New SaveFileDialog
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to save the file using (WriteAllText),
' and store all the text in txtEdit
My.Computer.FileSystem.WriteAllText(save1.FileName, TxtEdit.Text, False)
FilePath = save1.FileName
End If
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
'//Reset Filepath if clear box as new file
Dim intReply As Integer
If TxtEdit.Modified Then
intReply = MsgBox("The File has not been saved. Discard the change ", MsgBoxStyle.YesNoCancel)
If intReply = MsgBoxResult.Yes Then
TxtEdit.Clear()
FilePath = ""
End If
Else
TxtEdit.Clear()
FilePath = ""
End If
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'//Set Path to the Filename of the file being opened - Only set if filename is selected
Dim open1 As New OpenFileDialog
'display only text files in the dialog
open1.Filter = "Text files|*.txt"
'Show the dialog and see if the user clicked 'open' or 'cancel'.
' Result 'OK' means Yes/Open/Okay etc.
If open1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to open the file using (ReadAllText),
' and store all the text in txtEdit.text
TxtEdit.Text = My.Computer.FileSystem.ReadAllText(open1.FileName)
FilePath = open1.FileName
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim save1 As New SaveFileDialog
'// If filepath is set then save to this file otherwise prompt for filepath
If FilePath.Length = 0 Then
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Use the filename to save the file using (WriteAllText),
' and store all the text in txtEdit
My.Computer.FileSystem.WriteAllText(save1.FileName, TxtEdit.Text, False)
FilePath = save1.FileName
End If
End If
If FilePath.Length > 0 Then
My.Computer.FileSystem.WriteAllText(FilePath, TxtEdit.Text, False)
End If
End Sub
End Class
LilleMonster
You need to look at your code
My.Computer.FileSystem.WriteAllText
doesnt return any values so am unsure of what your really trying to do here - look in the help for details on this method
What your probably trying to do is save the existing file contents and then Load the textbox with the file being requested. Two distinct actions
My.Computer.FileSystem.WriteAllText(FilePath, txtEdit.Text, False) '//Save existing file
txtEdit.Text = My.Computer.FileSystem.ReadAllText(open1.FileName) '//Load File
steve_h
From looking at your code I would assume that you already have a SaveAs function, so I would assume that this is will save the contents with the existing filename if there was one specified (Open File) or prompt for one if one hasnt yet been given (New File)
Something like the following should work where FilePath is the filename that is currently open. On the new action I would specifically set Filepath = "" so this will work and when you open a file, filepath is already being set.
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
if FilePath.length = 0 then
Dim save1 As New SaveFileDialog
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
FilePath = Save1.Filename
End if
end if
If FilePath.length > 0 then
My.Computer.Filesystem.WriteAllText(FilePath,TxtEdit.text, False)
End if
End Sub
Stick to one method and use this in your application (Unless it doesnt give you what you need). For what your describing your already using my.computer.filesystem.writealltext
what makes you want to use streamreader There are numerous ways of writing a file - sticking to one in your application rather than using 2 or 3 different methods will make it easier for you to understand the code when you come to debug it.
Jason N. Gaylord
I understand what you are saying. I see the mistake that I made. I have looked into help, as well as the MSDN for Visual Basic regarding my.computer.filesytem.writealltext
I know what it is supposed to be doing. But for some reason it is not doing it.
Is it working for you
Currently I under the impression that the code works like this:
Dim reply1 As Integer Sets reply1 as an integer
If txtEdit.Modified Then MsgBox ("The file has not been saved. Save ",_
MsgBoxStyle.YesNoCancel) Determines if the txtEdit has been modified, if it has prompt with message box to save or not.
If reply1 = MsgBoxResult.Yes Then If the user selects yes from MsgBox then
My.Computer.Filename.WriteAllText (FilePath, txtEdit.Text, False) This causes the program to Write the file into the FilePath (which is determined before this code), the text from the Text Box txtEdit.Text, and does not append the text but overwrites it.
End If
This is not working for me. When I first open an txt file add "This is a Test" to it then open another file, it does prompt to save, then immediately goes to the Open Dialog Box, I open another file, to replace the text then I re-open the original text and the line of text that I placed in it is not there.
What am I not getting
Thanks
LDavis501
Harkernator
Yes, Just below the Public Class Form1, where it is supposed to be correct
Sujithf
BAM....Okay I got it, I got it. So when I am trying to save text and I am using a specific name then I must always use "FilePath =" in the appropriate areas based on what I am trying to do in whatever specific Sub.
Most of my problem is that I do not completely understand exactly everything that every syntax can and cannot do.
Can you recommend any books that I could get that would be almost like a dictionary or glossary type of deal that I can look up syntax and get the definition of them
Thanks,
LDavis501
jfoster77
Spotty,
Thanks for the help. Let me see if I understand this:
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
if FilePath.length = 0 then This specifies that if the FilePath is name is 0 in length then to perform the following below:
Dim save1 As New SaveFileDialog Sets save1 as a New SaveFileDialog
'display only text files in the dialog
save1.Filter = "Text files|*.txt"
If save1.ShowDialog() = Windows.Forms.DialogResult.OK Then
FilePath = Save1.Filename
End if
end if
If FilePath.length > 0 then
My.Computer.Filesystem.WriteAllText(FilePath,TxtEdit.text, False)
End if
End Sub
What I do not get is the FilePath problem. Is it being set in my code in the following instances
Public FilePath As String = ""
FilePath = open1.FileName
If according to the above code that we are looking to see if the FilePath.Length = 0 and this is not true as set by the Open Sub FilePath = open1.FileName. It then should then skip the first statement of this sub and go directly to the second If..Then Statement where it should just overwrite the existing file with the new file.
I cut and pasted the code you provided, opened a text file from my testing this code. I then added text to it and then tried the File>Save and it still pulled up the Save Dialog.
What am I missing
Thanks
LDavis501
Or what
Pure Krome
Filepath is really a variable that is holding the currently being worked on filename.
new document - we dont have a name
open document we do have a name (the one being opened)
save - we are saying if we have a name then use it otherwise we will ask.
Saveas - we are always asking.
Ebook
http://msdn.microsoft.com/vbasic/learning/introtovb2005/
and there should also have been another link to an ebook when you registered VB Express sent to you for the following ebook "MicrosoftR Visual BasicR 2005 Express Edition: Build a Program Now!"
Other resources (A great series of webcasts / video etc.)
http://msdn.microsoft.com/vstudio/express/vb/learning/default.aspx
MSDN and the help files will show specific syntax of methods, there are just too many methods, properties, classes etc. in the .NET framework for the book to be relevent and useful. So I'd suggest that you use the web and forums as well.
ElliotHC
Hmmm...Okay I will look into that and digest it. In the meanwhile, I am getting an error in the following (issue highlighted in yellow)
Private Sub mnuOpen_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuOpen.Click
Dim open1 As New OpenFileDialog
'display only text files in the dialog
open1.Filter =
"Text files|*.txt"FilePath = open1.FileName
'Show the dialog and see if the user clicked 'open' or 'cancel'. ' Result 'OK' means Yes/Open/Okay etc. If open1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Use the filename to open the file using (ReadAllText), ' and store all the text in txtEdit.text If txtEdit.Modified Then If MsgBox("The file has not been saved. Save ", _MsgBoxStyle.YesNoCancel) = MsgBoxResult.Yes
Then My.Computer.FileSystem.WriteAllText(FilePath, txtEdit.Text, False) End IftxtEdit.Text =
My.Computer.FileSystem.ReadAllText(open1.FileName)txtEdit.Modified =
FalseFilePath = open1.FileName
End SubI am getting the following: Argument cannot be Nothing.
Parameter name: file --- In regards to that portion.
While I understand that it is refering to the fact that a reference to an object does not exist, b/c I havent programmed it, I dont understand where/what I missed.
jayakhanna
Being the Absolute beginner that I am I am not fully understanding the txtEdit.Modified Statement. Could I trouble you to explain that to me and why you used it
BTW.....where can i send my tution to
Thanks
LDavis501
abowman
Spotty, (and everyone else)
Thanks for all the help to a real, real beginner to programing in general, I apl;ogize if my questions and situations seem rudimentary, its just that I want to understand everything I am doing so that I do not have to ask later. I really appreciate your help.
And on that note......
I am trying to get the OpenFileDialog to prompt you if you opened a text file, made some changes then decided to open a new file, before saving as or saving.
I figured this is what I need to use:
Dim
reply1 As Integer If txtEdit.Modified Then MsgBox("The file has not been saved. Save ", _MsgBoxStyle.YesNoCancel)
If reply1 = MsgBoxResult.Yes ThentxtEdit.Text =
My.Computer.FileSystem.WriteAllText(open1.FileName, txtEdit.Text, False) End IfHowever, this statement:
My.Computer.FileSystem.WriteAllText(open1.FileName, txtEdit.Text, False)
returns an error:
"Expression does not return a value."
Ideas