Hi All,

I have written a text editor for an assignment and have found a few faults with it im hoping someone could help me out.

First one is my save menu option, when I click Save in the file menu, it brings up the save as dialog box as apposed to just updating the file. Please see below the code I have for it, if anyone could tell me what I need to change my code to it would be much appreciated.

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
        Dim CurrentFile As String = "Untitled Document"
        If CurrentFile <> "Untitled Document" Then
            My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
        Else
            SaveAsToolStripMenuItem.PerformClick()
        End If
    End Sub

Second thing is, I have set up a customise menu so that you can change the font type, font colour, but what seems to be happening is that I highlight the text I want to change select my changes through my menu options but all the text on the page changes? Below is the code I have written, can anyone tell me what I need to change to make this only change the text I highlight.

Private Sub SetFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetFontToolStripMenuItem.Click

        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtBox.Font = FontDialog1.Font
        End If

    End Sub

    Private Sub SetFontColourToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetFontColourToolStripMenuItem.Click

        If ColorDialog1.ShowDialog = DialogResult.OK Then
            txtBox.ForeColor = ColorDialog1.Color
        End If

    End Sub

Any help would be greatley appreciated

John

Hi,
To create a texteditor you should use a richtextbox in stead of a textbox.
But you can try the codes I gave you for a richtextbox !

For the textcolor, change the code into this:

If ColorDialog1.ShowDialog = DialogResult.OK Then            
txtBox.SelectionColor = ColorDialog1.Color        
End If

For the font , change it into this:

If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtBox.SelectionFont = FontDialog1.Font
        End If

Hi,
To create a texteditor you should use a richtextbox in stead of a textbox.
But you can try the codes I gave you for a richtextbox !

For the textcolor, change the code into this:

If ColorDialog1.ShowDialog = DialogResult.OK Then            
txtBox.SelectionColor = ColorDialog1.Color        
End If

For the font , change it into this:

If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtBox.SelectionFont = FontDialog1.Font
        End If

Works great, I didnt realise that a rich text box would make such a difference.

Thank you very much for your help.

Just need to solve the save problem then I am sorted.

Thanks again

John

Hi,

If you want to thank me use the arrow keys :)

Now for the saving problem you can try this:

Dim strFileName As String
              Dim strFilePath As String = ".\"
    
        SaveFileDialog1.Filter = "RTF Files|*.rtf|All files|*.*"
        SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True

        ' Save the file
        If strFileName Is Nothing Then
            If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                txtBox.SaveFile(Me.SaveFileDialog1.FileName)
                strFileName = Me.SaveFileDialog1.FileName
            End If
        Else
            txtBox.SaveFile(strFileName)
        End If

I hope it helps.

Hi,

If you want to thank me use the arrow keys :)

Now for the saving problem you can try this:

Dim strFileName As String
              Dim strFilePath As String = ".\"
    
        SaveFileDialog1.Filter = "RTF Files|*.rtf|All files|*.*"
        SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True

        ' Save the file
        If strFileName Is Nothing Then
            If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                txtBox.SaveFile(Me.SaveFileDialog1.FileName)
                strFileName = Me.SaveFileDialog1.FileName
            End If
        Else
            txtBox.SaveFile(strFileName)
        End If

I hope it helps.

I re-wrote the code and I get a blue line under "SaveFileDialog" in the statement with the message that says "Reference to a non shared member requires an object reference" please see the code I have used below

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        Dim strFileName As String
        Dim strFilePath As String = ".\"
        SaveFileDialog.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveFileDialog.FilterIndex = 2
        SaveFileDialog.RestoreDirectory = True


        If strFileName Is Nothing Then
            If SaveFileDialog.ShowDialog = DialogResult.OK Then
                txtBox.SaveFile(Me.SaveFileDialog.FileName)
                strFileName = Me.SaveFileDialog.FileName
            End If
        Else
            txtBox.SaveFile(strFileName)
        End If
    End Sub
End Class

Im not sure what the message means, have you got any idea's.

Cheers

John

solved that issue, I didnt have a savefiledialog added to my project, however I am still getting the save file dialog box when I press the save button, even though the file has already been saved it wont update the existing file. Has anyone got any idea's.

Thanks

John

Perhaps you should migrate the code you have now to a "save as" option, and under save have some type of variable that holds the current file name and overwrite the file with whatever is in the richtextfield (no idea what code would be needed, but it is an idea (no matter how obvious)

I will give it a blast, thanks for the thaught.

Johno

Ok, I had a go at changing the save as sub routine, the save as menu item works fine, but the save menu item doesnt, take a look at my code, can anyone tell me what I am doing wrong.

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim CurrentFile As String
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
            Else
                My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
            End If
        Catch ex As Exception

        End Try
    End Sub

Cheers

John

Ok I have had a look at this from another angle, I changed the code in the save as sub routine, I changed the name of the save as menu item to just save, I re-wrote my code so it now looks like this

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
        Dim CurrentFile As String
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
            Else
                My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
            End If
        Catch ex As Exception

        End Try
    End Sub

But I am still getting the same result as before, it saves with the save file dialog box as it should but then instead of just updating the current file it opens the save file dialog box.

I have drawn a proper blank on this if anyone could help me out it would be greatley appreciated.

John

Hi John,

I did some testing and this works like it should, that means that if the file exist it only saves the new added sentences.
This is the tested codes:

Dim strFileName As String
    Dim strFilePath As String = ".\"
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       
        SaveFileDialog1.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True


        If strFileName Is Nothing Then
            If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                txtBox.SaveFile(Me.SaveFileDialog1.FileName)
                strFileName = Me.SaveFileDialog1.FileName
            End If
        Else
            txtBox.SaveFile(strFileName)
        End If
    End Sub

' this part of code I used for the Save As event.

 Private Sub btnsaveas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsaveas.Click

        SaveFileDialog1.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"         SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True

        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            txtBox.SaveFile(Me.SaveFileDialog1.FileName)
            strFileName = Me.SaveFileDialog1.FileName
        End If
    End Sub

Thanks for your sharing! I have learnt so much from your post!

Hi,

No problem, glad to help, use the arrow to vote the answers.

Ok it works its fixed its sorted COME ON YOU BEAUTY, see code below

Open

Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Open.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        Open.Title = "Open"
        Try
            If Open.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtBox.Text = My.Computer.FileSystem.ReadAllText(Open.FileName)
                Me.Text = Open.FileName + " - Text Editor"
                FP = Open.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub

Save As

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"

        If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
            My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
            Me.Text = SaveAs.FileName + " - Text Editor"
            FP = SaveAs.FileName
        End If

    End Sub

Save

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        If FP = "" Then
            SaveAsToolStripMenuItem.PerformClick()
        Else
            My.Computer.FileSystem.WriteAllText(FP, txtBox.Text, False)
        End If

    End Sub

Thanks for everyones advise on this its been a tough one.

Thanks agaon

John

Hi John,

Glad it works now :)

Mark this thread as resolved.

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.