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.

See if this helps.

'//----------- Pre-requisites: 3 Buttons, 1 RichTextBox. ---------------\\
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "New"
        Button2.Text = "Open"
        Button3.Text = "Save" : Button3.BackColor = Color.LightGreen
        RichTextBox1.Tag = "new" '// set .Tag to determine as New File.
    End Sub
    '// new file.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Clear() '// clear RichTextBox.
        RichTextBox1.Tag = "new" '// set .Tag to determine as New File.
        Button3.BackColor = Color.LightGreen
    End Sub
    '// open file.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            If ofd.FileName.ToLower.EndsWith(".rtf") Then '// determine if a .RTF File.
                RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText)
            Else
                RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText)
            End If
            RichTextBox1.Tag = ofd.FileName '// set .Tag to FileName.
            Button3.BackColor = Color.LightGreen
        End If
    End Sub
    '// save file.
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Not RichTextBox1.Tag = "new" Then '// check if .Tag is NOT a NEW File.
            If RichTextBox1.Tag.ToString.ToLower.EndsWith(".rtf") Then '// determine if a .RTF File.
                RichTextBox1.SaveFile(RichTextBox1.Tag.ToString, RichTextBoxStreamType.RichText)
            Else
                RichTextBox1.SaveFile(RichTextBox1.Tag.ToString, RichTextBoxStreamType.PlainText)
            End If
            Button3.BackColor = Color.LightGreen
            MsgBox("File Saved.")
            Exit Sub '// skip the SaveFileDialog.
        End If

        Dim sfd As New SaveFileDialog
        sfd.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            If sfd.FileName.ToLower.EndsWith(".rtf") Then '// determine if a .RTF File.
                RichTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText)
            Else
                RichTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText)
            End If
            RichTextBox1.Tag = sfd.FileName '// set .Tag to FileName.
            Button3.BackColor = Color.LightGreen
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Button3.BackColor = Color.Orange
    End Sub
End Class
commented: Couldn't be more helpful! +2

Your code is perfectly written but the only problem I have is that I can't find a way to fit it in. I'm probably asking too much, but I would really appreciate you taking a look at the whole application code and telling me where to add your code in a manner so it complies with the rest of my code. Thank you.

Here is a link to the project, compressed in a .RAR file:
http://h1.ripway.com/JimmyIliadisInc/Project%20Source.rar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.