Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

vb.net file open

  • 11-03-2009 12:28AM
    #1
    Registered Users, Registered Users 2 Posts: 5,981 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 5,981 ✭✭✭Caliden


    Doh!

    Cant believe I missed that. Cheers. Works perfectly now


Advertisement