2 Replies - 629 Views - Last Post: 10 June 2015 - 08:07 PM Rate Topic: -----

#1 Vancelle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-June 15

Can't use the "Open" button to open a file into another fo

Posted 10 June 2015 - 06:02 PM

I am trying to create a "Notepad"-esque type program. I have an "Open" menu item that I'm trying to use to open a file not in the form that the "Open" menu item is located, but make it open into another form.

My main form contains a menustrip with "File" and "Help". There is an "Open" and "Exit" file-item under the "File" button and "About" under the "Help" button. I also have a Treeview where I want to make it to where the items created in this program can be displayed and opened from. There is also one button, which will be for opening another form which is basically "Notepad".

Another problem I have been running into is that I can't seem to get my "Save As" to work correctly. It requires me to already have a text file and then to select it and overwrite it in order to save.

I will attach the code that I have for each of these things. I don't have any for Treeview yet because I still haven't fully wrapped my head around that one yet.


This is my Main form, the one that displays when the program starts and the main hub.
Public Class Main



    Public Sub btnNewOrganizer_Click(sender As Object, e As EventArgs) Handles btnNewOrganizer.Click
        'Create a new Organizer form.
        Dim frmNew_Organizer_Input_Form As New New_Organizer_Input_Form

        'Display the form in modal style.
        frmNew_Organizer_Input_Form.ShowDialog()
    End Sub

    'Display an about box.
    Private Sub mnuHelpAbout_Click(sender As Object, e As EventArgs) Handles mnuHelpAbout.Click
        MessageBox.Show("Anthony's All-In-One Organizer version 1.0")
    End Sub

    'Open any type of ".txt or .rtf" file and edit it.
    Private Sub mnuFileOpen_Click(sender As Object, e As EventArgs) Handles mnuFileOpen.Click

    End Sub

    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub ofdOpenFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ofdOpenFile.FileOk
      
    End Sub
End Class



This is my second form, the one that is pretty much Notepad and the one where I want to edit everything, even what is opened in the main form.
Public Class New_Organizer_Input_Form
    'Class-level variables
    Private strFilename As String = String.Empty 'Document filename


    'Make the current selection Bold.
    Private Sub mnuFormatBold_Click(sender As Object, e As EventArgs) Handles mnuFormatBold.Click
        If mnuFormatBold.Checked = True Then
            RichTextBox1.SelectionColor = Color.Black
            RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)        
        End If
    End Sub

    'Make the current selection Italic.
    Private Sub mnuFormatItalic_Click(sender As Object, e As EventArgs) Handles mnuFormatItalic.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
    End Sub

    'Make the current selection Underlined.
    Private Sub mnuFormatUnderline_Click(sender As Object, e As EventArgs) Handles mnuFormatUnderline.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Underline)
    End Sub

    'Make the current selection a Header.
    Private Sub mnuFormatHeader_Click(sender As Object, e As EventArgs) Handles mnuFormatHeader.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font.Name, 15, FontStyle.Bold)
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
        'Enable the Undo Button.
        mnuEditUndo.Enabled = RichTextBox1.CanUndo
        'Enable the Redo Button.
        mnuEditRedo.Enabled = RichTextBox1.CanRedo
    End Sub


    Private Sub mnuEditUndo_Click(sender As Object, e As EventArgs) Handles mnuEditUndo.Click
        'Create an Undo Button
        RichTextBox1.Undo()
    End Sub


    Private Sub mnuEditRedo_Click(sender As Object, e As EventArgs) Handles mnuEditRedo.Click
        'Create a Redo Button.
        RichTextBox1.Redo()
    End Sub

    Private Sub mnuFileSaveAs_Click(sender As Object, e As EventArgs) Handles mnuFileSaveAs.Click
        'Create a SaveFileDialog.
        Dim sfdSaveFileAs As New SaveFileDialog
        'If the user names the file and hits Ok.
        If sfdSaveFile.ShowDialog = DialogResult.OK Then
            'Create a writer to write to the file.
            Dim writer As New System.IO.StreamWriter(sfdSaveFileAs.FileName, False)
            'Write the richtextbox's content to the file.
            writer.Write(RichTextBox1.Text)
            'Close the writer.
            writer.Close()
        End If
    End Sub

    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
        'Close the file.
        Me.Close()
    End Sub

    Private Sub sfdSaveFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles sfdSaveFile.FileOk

    End Sub

    Private Sub mnuFilePrint_Click(sender As Object, e As EventArgs) Handles mnuFilePrint.Click
        'Print the current document.
        pdPrint.Print()
    End Sub

    Private Sub pdPrint_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles pdPrint.PrintPage
        'Print the contents of the text box.
        e.Graphics.DrawString(RichTextBox1.Text, New Font("MS Sans Serif", 12, FontStyle.Regular),
                              Brushes.Black, 10, 10)
    End Sub

    Private Sub mnuFormatColor_Click(sender As Object, e As EventArgs) Handles mnuFormatColor.Click
        'Show the color changing dialog box.
        cdColor.ShowDialog()

        'Change to font color to the color selected.
        If cdColor.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.ForeColor = cdColor.Color
        End If
    End Sub

    Private Sub mnuFormatFont_Click(sender As Object, e As EventArgs) Handles mnuFormatFont.Click
        'Show the font changing dialog box.
        fdFont.ShowDialog()

        'Change the font to the font you select.
        If fdFont.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.Font = fdFont.Font
            RichTextBox1.ForeColor = fdFont.Color
        End If
    End Sub

    Private Sub mnuEditCopy_Click(sender As Object, e As EventArgs) Handles mnuEditCopy.Click
        'Copy the selected text.
        RichTextBox1.Copy()
    End Sub

    Private Sub mnuEditPaste_Click(sender As Object, e As EventArgs) Handles mnuEditPaste.Click
        'Paste the selected text.
        RichTextBox1.Paste()
    End Sub

    Private Sub mnuEditSelectAll_Click(sender As Object, e As EventArgs) Handles mnuEditSelectAll.Click
        'Select all of the text.
        RichTextBox1.SelectAll()
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Can't use the "Open" button to open a file into another fo

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,542
  • Joined: 12-December 12

Re: Can't use the "Open" button to open a file into another fo

Posted 10 June 2015 - 06:31 PM

If sfdSaveFile.ShowDialog = DialogResult.OK Then

You have mis-spelt the name of this dialog, it is sfdSaveFileAs. But the IDE should be showing you errors for this..?

I don't know if you are, but you should also be entering (or supplying) the file extension when using the SaveFile dialog.

I cannot see your code attempt for your Open question..?
frmNew_Organizer_Input_Form.ShowDialog()

You are opening the second form modally. After this, you can check a property of the second form to obtain the chosen filename and path, if one was chosen, and open the file.

A tutorial:

Passing Data Between Forms

This post has been edited by andrewsw: 10 June 2015 - 06:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 Vancelle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-June 15

Re: Can't use the "Open" button to open a file into another fo

Posted 10 June 2015 - 08:07 PM

Thank you for pointing out my errors. I will read your tutorial and try to go from there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1