2017-05-17 13 views
2

질문하기 전에 인터넷에서 많은 검색을 수행했지만 내 문제에 대한 답변을 찾지 못했습니다. 버튼이 하나 있고 클래스가 두 개인 Form이 하나 있습니다. Form의 Button을 누르면 디스크에서 file.txt를 선택하는 대화 상자가 열립니다. 만약 Open Button을 누르면 프로그램은 코드에 쓰여진 것을 정확하게 수행합니다. 취소 단추를 누르면 프로그램에서 오류가 발생합니다. System.NullReferenceException : '개체 인스턴스에 대한 개체 참조가 설정되어 있지 않습니다.' 경로는 아무것도 아니었다. (On Class FilePan nomeFilePan = path.Replace ("1319", "panasonic \") + _nomeFile + ".pan")openFileDialog에서 취소 버튼을 누르면 오류가 나타나지 않습니다.

프로그램을 다시 Form으로 보내려면 어떻게해야합니까? 버튼을 눌러 파일을 열거 나 프로그램을 닫으시겠습니까?

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 
+0

같은 것을 시도'취소 할 경우 then' 뭔가를하거나 허용되지 않는 취소하지 않으면 당신은 당신의 else 문 – Werdna

+0

@Werdna에'Return'을 넣을 수 있습니다. Else 문장에서 반환 같은 오류 –

답변

1

원하는대로 할 수있는 방법이 많이 있습니다. 당신이 당신의 현재 구조를하고 싶은 경우에 당신은 시도 할 수 : 확인 대화 상자를 클릭하지 않은 경우

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 

이 거짓의 부울을 반환합니다. 그런 다음 .SetFileTxt()를 if 문에 넣습니다.

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 
+0

생산이 내 질문을 해결했다. 고맙습니다. –

+0

불리언 방법을 사용하지 않는 방법이 있습니까? –

+1

부울을 반환하는 대신 사용자가 취소를 클릭하면 값을 설정할 수 있습니다. 경로, nomeFile, nomeFileTxt는 빈 문자열이됩니다. 그런 다음 해당 문자열이 비어 있는지 확인하거나 대화 상자가 취소되었는지 확인하지 않습니다. – Neal