Question

I'm working on an assignment and I need to use an OpenFileDialog Class to choose a .txt file and return that file name property to a function called GetFileName()

Now this is the code I have for my button click:

    Private Sub 
    btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click

End Sub

I'm confused as to how I get the file I selected into my function. This is the code for my function.

     Private Function GetFileName() As String

     Dim OpenDonorList As New OpenFileDialog

    OpenDonorList.Filter = "txt files (*.txt)|*.txt"
    OpenDonorList.Title = "Save File As"
    OpenDonorList.InitialDirectory = "C:\"
    OpenDonorList.RestoreDirectory = True

    DialogResult = OpenDonorList.ShowDialog

    If DialogResult = Windows.Forms.DialogResult.OK Then



    Return ?
    End Function

How do I get my OpenDonorList .txt file to my GetFileName() Function?

Thanks!

Was it helpful?

Solution

It seems to me like your unsure on how to call the function from the button click (I might be wrong). So first when you call a function it must always return a value (from the return keyword).

You've setup a function that displays a OpenFileDialog - so what value should it return? It should return a path & filename. This can just be stored in a string varaible.

So, a bit of tweaking to your code might fix it.

Heres an example:

On your button code, You want to call the actual function plus a variable to store the path name (as above a string) Simply:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim thePathName As String = GetFileName()
    MessageBox.Show(thePathName)
End Sub

Now, In the function you want to add the return. There is a problem with your if-statement (no end if). If the result is OK then return the path name. Otherwise return null and call an error (you can change this):

Private Function GetFileName() As String

    Dim OpenDonorList As New OpenFileDialog

    OpenDonorList.Filter = "txt files (*.txt)|*.txt"
    OpenDonorList.Title = "Save File As"
    OpenDonorList.InitialDirectory = "C:\"
    OpenDonorList.RestoreDirectory = True

    DialogResult = OpenDonorList.ShowDialog

    If DialogResult = Windows.Forms.DialogResult.OK Then
        Return OpenDonorList.FileName
    Else
        MessageBox.Show("Error!")
        Return vbNull
    End If

End Function

In the button code you can then call another routine that uses the value of thePathName to do something, such as open the file for reading. In the example above it will just show a messagebox with the path name to the selected file.

Hope this helps.

OTHER TIPS

You already have most of the code, all you have to do is to use FileName property to return the selected file:

If DialogResult = Windows.Forms.DialogResult.OK Then
    Return OpenDonorList.FileName
End If

I would also add this line to settings of your open file window to make sure only one file can be selected:

OpenDonorList.Multiselect = False
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fileName As String = GetFileName()
    If (String.IsNullOrEmpty(fileName)) Then
        MessageBox.Show("No file was selected.")
    Else
        MessageBox.Show(String.Format("You selected file: {0}", fileName))
    End If
End Sub

Private Function GetFileName() As String
    Dim openDonorList As New OpenFileDialog()
    With openDonorList
        .Filter = "txt files (*.txt)|*.txt"
        .Title = "Save File As"
        .InitialDirectory = "C:\"
        .RestoreDirectory = True
    End With
    Dim result As DialogResult = openDonorList.ShowDialog()
    If result = Windows.Forms.DialogResult.OK Then Return openDonorList.FileName
    Return Nothing
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top