0

Visual Basic で作成しているアプリケーションからの保存を処理するために、[名前を付けて保存] ダイアログ ボックスを作成しようとしています。

これは私が使用しているコードであり、コードも理解していません:

Private Sub MenuExportTo_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MenuExportTo.Click

        Dim myStream As IO.Stream  'I don't understand this line
        Dim saveFileDialog1 As New SaveFileDialog() 'I don't understand this line


        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
        'I understand this
        saveFileDialog1.FilterIndex = 1 ' I understand this
        saveFileDialog1.RestoreDirectory = True ' I understand this

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then 
            ' I don't understand the rest

            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here. (This was what the example 
I referenced put here. the important code goes here, but the example was no help)
                myStream.Close()
            End If
        End If

問題 1: 私はこれに慣れていません。保存方法で実際に何が起こっているのかを理解するのに十分な詳細な説明を提供してくれる情報源はありません。しっかりとした説明が必要です。ここで迷っています。

問題 2: コードをテキスト ファイルの特定の場所に書き込むことはできますが、リターン キャレットが失われ、次を使用してすべて 1 行でテキスト ファイルに保存されます。

My.Computer.FileSystem.WriteAllText("c:\savelocation", TextBox1.Text, False

戻りキャレットの問題を修正するにはどうすればよいですか? また、コードの一部ではなく、ユーザーが保存場所を選択できるようにするにはどうすればよいですか?

問題 3: ユーザーが [名前を付けて保存] ダイアログで指定したファイル パスを変数に取得するにはどうすればよいですか?

私の質問の1つに対する答えだけでも、どんな助けでも大歓迎です! ありがとう!

4

2 に答える 2

3

この例が役立つかどうかを確認してください。

Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd.FilterIndex = 1
    sfd.RestoreDirectory = True
    If sfd.ShowDialog() = DialogResult.OK Then
        Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
        Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
        sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
        sw.Close() ' close the file
    End If
End Sub
于 2013-05-04T02:50:57.423 に答える
0
Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd.FilterIndex = 1
    sfd.RestoreDirectory = True
    If sfd.ShowDialog() = DialogResult.OK Then
        Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
        Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
        sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
        sw.Close() ' close the file
    End If
End Sub
于 2014-01-10T07:36:56.843 に答える