Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

vb.net file open

Options
  • 11-03-2009 12:28am
    #1
    Registered Users Posts: 5,982 ✭✭✭


    Ok I was asked to create a little app that will open and save text files.

    Everything is working great but with my filters for my file open dialog I cant see text files even though I have it declared.
    All files works fine but not text files.
    Any idea where I'm going wrong?

    here is my filter for the fileopendialog:
    Text Files(*.txt)| .txt|All Files(*.*)|*.*

    With this, I cant see text files when I first open the dialog but if I set it to all files I can see my text file and open it fine. Saving is no problem either



    Here is the code:

    Imports System.IO
    Imports Microsoft.VisualBasic.ControlChars
    
    Public Class Form1
    
        Dim clip As String = ""
        Dim fname As String = ""
        Dim sname As String = ""
    
    
    
        Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
            txtInput.SelectedText = clip
        End Sub
    
    
    
        Private Sub mnuCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCut.Click
            clip = txtInput.SelectedText
            txtInput.SelectedText = ""
        End Sub
    
        Private Sub mnuConcatCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuConcatCut.Click
            clip = clip + txtInput.SelectedText
            txtInput.SelectedText = ""
        End Sub
    
        Private Sub mnuClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClear.Click
            clip = ""
        End Sub
    
        Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
            clip = txtInput.SelectedText
        End Sub
    
        Private Sub mnuConcatCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuConcatCopy.Click
            clip = clip + txtInput.SelectedText
    
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
            End
        End Sub
    
        Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
            Dim instream As StreamReader
    
            fdOpen.ShowDialog()
            fname = fdOpen.FileName
    
            Try
                instream = File.OpenText(fname)
            Catch
                MsgBox("File does not exist!! --" & fname, MsgBoxStyle.Critical)
                Return
            End Try
            Dim line As String
            line = instream.ReadLine()
            While line <> Nothing
                txtInput.AppendText(line & NewLine)
                line = instream.ReadLine
            End While
            instream.Close()
    
        End Sub
    
    
        Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
            Dim outstream As StreamWriter
    
            fdSave.ShowDialog()
            sname = fdSave.FileName
    
            Try
                outstream = File.CreateText(sname)
            Catch
                MsgBox("Cannot open file for output! -- " & sname, MsgBoxStyle.Critical)
                Return
            End Try
    
            outstream.WriteLine(txtInput.Text)
            outstream.Close()
    
        End Sub
    
        
    End Class
    
    
    
    
    
    


    Any help is appreciated


Comments

  • Registered Users Posts: 2,894 ✭✭✭TinCool


    Try this syntax for the FileDialog.Filter property:
    "Text Files(*.txt)| *.txt|All Files(*.*)|*.*"
    

    You were missing the "*" from your Text Files filter.


  • Registered Users Posts: 5,982 ✭✭✭Caliden


    Doh!

    Cant believe I missed that. Cheers. Works perfectly now


Advertisement