Results 1 to 9 of 9

Thread: [2005] open file dialog problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    [2005] open file dialog problem

    I am trying to use the open dialog to allow the user to select the file being computed. I think my code is right, but visual basic says that 'type stream has not been defined'.

    Any help is appreciated. Code below.

    champ0342



    Code:
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim ofd As New OpenFileDialog()
    
        ofd.InitialDirectory = "c:\"
        ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        
        If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = ofd.OpenFile()
                If (myStream IsNot Nothing) Then
                    
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Try another file)
            Finally
                If (myStream IsNot Nothing) Then
                    
                End If
            End Try
        End If
    End Sub

  2. #2
    New Member
    Join Date
    Dec 2007
    Posts
    4

    Re: [2005] open file dialog problem

    Dim fileStream As System.IO.FileStream = Nothing
    try that

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Re: [2005] open file dialog problem

    Thanks. That kinda did it, but I had to change one part:
    Code:
    Dim fileStream As System.IO.Stream = Nothing
    by making it system.io.stream instead of system.io.filestream it works under option strict.

  4. #4
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] open file dialog problem

    try this

    Code:
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ofd As New OpenFileDialog()
    
        Dim MyValue As String = ""
        ofd.InitialDirectory = "c:\"
        ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        
        If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Using sr As New StreamReader(ofd.FileName)
                MyValue = sr.ReadToEnd()
            End Using
        End If
    End Sub
    That should work, and if the file is not found
    use ofd.CheckFilePath, or a property with a similar name which checks the file exists...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Re: [2005] open file dialog problem

    A new challenger approaches.....

    when it opens the file it gives me an error says that ' the system cannot find the file .../bin/debug/system.io.filestream'


    does anyone know what that error means?

  6. #6
    New Member
    Join Date
    Dec 2007
    Posts
    4

    Re: [2005] open file dialog problem

    think its HAS TO BE
    system.io.FILESTREAM
    and not system.io.stream
    since you will be using all the funcitons that deals with FILES.

  7. #7
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] open file dialog problem

    Sorry, I forgot to mention, you need to do this at the top of your code (above everything)

    Imports System.IO

    or change the streamreader code to

    Code:
    Using sr As New System.IO.StreamReader(ofd.FileName)
                MyValue = sr.ReadToEnd()
            End Using

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Re: [2005] open file dialog problem

    I need to split the line into two sections. How do I do that. I tried this code:
    Code:
    MyValue = sr.ReadLine.Split
    but it said that: "value of a 1d array can not be converted to a string".

  9. #9
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] open file dialog problem

    Declare MyValue As An Array

    Dim MyValue() As String

    MyValue = sr.ReadLine.Split()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width