2017-05-17 6 views
2

尋ねる前に、私はインターネット上で多くの検索を行ってきましたが、私の問題に対する答えは見つかりませんでした。 ボタンと2つのクラスを持つフォームが1つあります。 フォーム上のボタンを押すと、ディスクからfile.txtを選択するダイアログが開きます。 Openボタンを押すと、プログラムはコードに書いていることを正しく実行します。 キャンセルボタンを押すと、プログラムがエラーになります。System.NullReferenceException: 'オブジェクトインスタンスへの参照が設定されていません。'パスは何もなかった。 (On Class FilePan nomeFilePan = path.Replace( "1319"、 "panasonic \")+ _nomeFile + ".pan")openFileDialogで[キャンセル]ボタンを押してもエラーが表示されません。

プログラムをフォームに戻すにはどうしたらいいですか?ボタンを押してファイルを開くか、プログラムを終了しますか?

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 

OKをダイアログ上でクリックされない場合、これは偽のブール値を返します。 その後、.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

関連する問題