Hello world of DaniWeb, what's up? I have the following problem; I've created a text editing application in VB.NET that actually works pretty well with opening, saving files etc. But when it comes to opening a file, modifying it, and then trying to save it, the save dialog will pop up despite the file having been saved before. Before you try to help me, I'd also like to say that there is no problem when just creating a file and then using the Save function (namely replacing the file saved before with its new, modified version). I know I may have baffled you a bit, but I don't think there's another way to explain it. Anyway, let's just jump straight into the code, it may help you out a bit.
So, to begin with, I have declared 2 non-Boolean values:
Dim Saved As Boolean = False
Dim DirectoryExists As Boolean = False
The first one, Saved, is used to determine whether text inside the RTB control is saved. This value also changes in accordance with the RichTextBox_TextChanged event. That means that, if we save some text, this value will change to,
Saved = True
but if we modify the text again, it'll then change to
Saved = False
As for the second one, DirectoryExists, it is used to determine whether the text had been previously saved, despite it having been modified. To make things even more clear to you, what I want to say is that when we save some text, after the lines of the saving command, those two values (Saved & DirectoryExists) will both be set to True, since the text is saved while a save location exists. Therefore that means:
' Insert code regarding "Save" function here.
DirectoryExists = True
Saved = True
Continuing with the problem, I've also set the above values to True when the file opening command is executed. Take a look at the code below for a better chance of understanding what I want to say:
OpenFile.InitialDirectory = "c:\"
OpenFile.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFile.FilterIndex = 1
OpenFile.RestoreDirectory = True
If (OpenFile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
If OpenFile.FilterIndex = 1 Then
RichTextBox1.LoadFile(OpenFile.FileName, RichTextBoxStreamType.RichText)
Me.Text = "" & OpenFile.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
ElseIf OpenFile.FilterIndex = 2 Then
RichTextBox1.LoadFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
Me.Text = "" & OpenFile.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
ElseIf OpenFile.FilterIndex = 3 Then
RichTextBox1.LoadFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
Me.Text = "" & OpenFile.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
End If
End If
I have deemed that these values should be set to True when loading a file, because the file itself is actually saved and a save location already exists when it is loaded. As you may have noticed, I haven't actually shown you the "problematic" code yet. That's because I wanted to make things as clear as possible to you and add my thoughts regarding what I did, instead of just rudely asking you to give me a solution. Enough with the blah blah, it is now time to solve this thing! OK, below you'll see the code for the Save function of the application in addition to what I also want to include (it will appear in comment format in the following code). Please have a look at it, and then continue reading before you try to find a solution for me.
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
If Saved = False Then
SaveText.InitialDirectory = "c:\"
SaveText.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*"
SaveText.FilterIndex = 1
SaveText.RestoreDirectory = True
If (SaveText.ShowDialog() = Windows.Forms.DialogResult.OK) Then
If SaveText.FilterIndex = 1 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.RichText)
Me.Text = "" & SaveText.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
ElseIf SaveText.FilterIndex = 2 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
Me.Text = "" & SaveText.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
ElseIf SaveText.FilterIndex = 3 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
Me.Text = "" & SaveText.FileName & " - NoteEditor Pro"
DirectoryExists = True
Saved = True
End If
End If
ElseIf Saved = True Then
If SaveText.FilterIndex = 1 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.RichText)
ElseIf SaveText.FilterIndex = 2 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
ElseIf SaveText.FilterIndex = 3 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
End If
ElseIf (Saved = False) And (DirectoryExists = True) Then
If SaveText.FilterIndex = 1 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.RichText)
ElseIf SaveText.FilterIndex = 2 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
ElseIf SaveText.FilterIndex = 3 Then
RichTextBox1.SaveFile(SaveText.FileName, RichTextBoxStreamType.PlainText)
End If
'ElseIf (Saved = False) And (DirectoryExists = True) And (Me.Text = "" & OpenFile.FileName & " - NoteEditor Pro") Then
'If OpenFile.FilterIndex = 1 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.RichText)
'ElseIf OpenFile.FilterIndex = 2 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
'ElseIf OpenFile.FilterIndex = 3 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
'End If
'ElseIf (Saved = True) And (Me.Text = "" & OpenFile.FileName & " - NoteEditor Pro") Then
'If OpenFile.FilterIndex = 1 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.RichText)
'ElseIf OpenFile.FilterIndex = 2 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
'ElseIf OpenFile.FilterIndex = 3 Then
'RichTextBox1.SaveFile(OpenFile.FileName, RichTextBoxStreamType.PlainText)
'End If
End If
End Sub
This code will throw an ArgumentException when we try to save the file that we just opened (if it is left unmodified) while if we just try to save it after editing it a bit, a new SaveFileDialog will pop up (while not a huge problem, it isn't supposed to appear since we actually want to replace the file with its modified version, instead of saving to a new file). Ah, I almost forgot, as you can see, I have 2 IF statements that contain AND (in addition to what the name of the form should be, if the code is to be executed). Either of those is supposed to be executed when trying to save a file that has been opened (and therefore has been saved before). Can you tell me what is the freakin' PROBLEM? I really spent hours trying to find a solution (both by using my brain and the Internet) but I can't seem to solve it. Have a nice day and thank you in advance.