Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Having touble with the SaveFileDialog

Hi, I'm using VB.NET2010 (VS2010), WinForms. I have a textbox that I'm trying to save to a file on the HDD. I'm actually able to succeed in saving the file, but there are some problems, which I list below. My code is in the code snippet below.

These are the problems:

1). After you enter a new file name into the SaveFileDialog "File Name" field, the file name changes to the fully qualified name of the file (includes the path). So, in my example below:

    File name - New Report
    Click SAVE
    File name becomes - C:\DATA\New Report.txt

How do I make it simply Save the file and close...which leads to the second problem.

2). The SaveFileDialog never closes. How do I make it close once I click SAVE?

You can click SAVE as many times as you want, the Dialog box does not close. I tried SaveFileDialog1.Close(), but that does not work.

If I force a close, the application's graphics seems to look like they needs to be refreshed.

Thank you,
Fulano


'// This event is triggered by clicking on the SAVE item of a MenuStrip on the Form.

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.DefaultExt = ".txt"
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.OverwritePrompt = True
        SaveFileDialog1.Title = "Save Report..."

        Me.SaveFileDialog1.ShowDialog()
        Me.Refresh()

    End Sub

'// This event is the actual SaveFileDialog Control.

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
          
             Try
                Dim swTargetFile As StreamWriter
                swTargetFile = New StreamWriter(SaveFileDialog1.FileName, True)
                swTargetFile.Write(tbxOutPut.Text)
                swTargetFile.Close()

            Catch ex As Exception
                MessageBox.Show("Error writing to file..." &  SaveFileDialog1.FileName)
            End Try
        End If

    End Sub

Open in new window

0
Mr_Fulano
Asked:
Mr_Fulano
  • 2
1 Solution
 
CodeCruiserCommented:
I think you code needs to be changed to below



'// This event is triggered by clicking on the SAVE item of a MenuStrip on the Form.

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.DefaultExt = ".txt"
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.OverwritePrompt = True
        SaveFileDialog1.Title = "Save Report..."

        If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
             Try
                Dim swTargetFile As StreamWriter
                swTargetFile = New StreamWriter(SaveFileDialog1.FileName, True)
                swTargetFile.Write(tbxOutPut.Text)
                swTargetFile.Close()

            Catch ex As Exception
                MessageBox.Show("Error writing to file..." &  SaveFileDialog1.FileName)
            End Try
        End If
        Me.Refresh()

    End Sub

Open in new window

0
 
Mr_FulanoAuthor Commented:
Aha, I see what I did...I need to sleep more and not stare at the computer screen for so many hours!

Yes, your solution is correct and it worked very well. Thank you for getting me across the finish line on that one.

Fulano
0
 
Mr_FulanoAuthor Commented:
Very good solutions! Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now