vb.netopenfiledialogcancel-button

How can I have no error when press Cancel Button on openFileDialog?


Before ask, I have done a lot of searches on the internet but have not found an answer to my problem. I have one Form with a button and two Classes. When I press the button on the Form, a Dialog opens to select a file.txt from the disk. If I press the Open Button, the program goes on correctly doing what is written on the code. If I press the Cancel Button, the program rises an exception:

System.NullReferenceException: ‘Reference to an object not set on an object instance.' Path was Nothing. (On Class FilePan nomeFilePan = path.Replace("1319", "panasonic") + _nomeFile + ".pan")

What can I do to send the program back to the Form, where I can press again the button to open a file or close the program?

Imports System
Public Class Form1
    Dim _fileTxt As FileTxt = New FileTxt()
    Dim CreaPan As FilePan = New FilePan()

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        _fileTxt.SetFileTxt()
        CreaPan.SetFilePan(_fileTxt.GetNomeFile(), _fileTxt.GetPath())
    End Sub
End Class


Public Class FileTxt
    Dim path As String
    Dim nomeFile As String
    Dim nomeFileTxt As String

    Public Sub SetFileTxt()
        Dim openFileDialog1 As New OpenFileDialog() With
            {
                .InitialDirectory = "",
                .Filter = "Txt file|*txt",
                .FilterIndex = 1,
                .RestoreDirectory = True,
                .Title = "Seleziona file"
            }
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            path = IO.Path.GetDirectoryName(openFileDialog1.FileName)
            nomeFile = IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName)
            nomeFileTxt = IO.Path.GetFileName(openFileDialog1.FileName)
        Else
            ' How can go back the program start (Form1 with the button)???
        End If
    End Sub

    Public Function GetNomeFile() As String
        Return nomeFile
    End Function

    Public Function GetPath() As String
        Return path
    End Function

End Class


Imports System.IO
Public Class FilePan
    Dim path As String
    Dim nomeFilePan As String

    Public Sub SetFilePan(ByVal _nomeFile As String, ByVal _path As String)

        path = _path
        nomeFilePan = path.Replace("1319", "panasonic\") + _nomeFile + ".pan"

        If File.Exists(nomeFilePan) Then
            File.Delete(nomeFilePan)
        End If
    End Sub
End Class

Solution

  • There are many ways to do what you want. If you want to do it with your current structure you could try:

    Public Function SetFileTxt() as Boolean
        Dim openFileDialog1 As New OpenFileDialog() With
            {
                .InitialDirectory = "",
                .Filter = "Txt file|*txt",
                .FilterIndex = 1,
                .RestoreDirectory = True,
                .Title = "Seleziona file"
            }
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            path = IO.Path.GetDirectoryName(openFileDialog1.FileName)
            nomeFile = IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName)
            nomeFileTxt = IO.Path.GetFileName(openFileDialog1.FileName)
            return True
        Else
            return False
        End If
    End Function
    

    This will return a bool of false if OK is not clicked on the dialog. Then put .SetFileTxt() in an if statment.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If _fileTxt.SetFileTxt() Then
            CreaPan.SetFilePan(_fileTxt.GetNomeFile(), _fileTxt.GetPath())
        End If
    End Sub