11 Replies - 1125 Views - Last Post: 28 April 2012 - 12:52 PM Rate Topic: -----

#1 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

how to use Save dialog in my program?

Posted 28 April 2012 - 05:17 AM

Hi from a VB newbie :/

I need help please, I have a button on my form and when clicked I want to show the save dialog box but I want to have a filter so the file saved can only be .txt also I want to change the default location of the save as dialog, is this possible?

so far I have the following

     Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
filesaveas.Filter = "C:\Test\"



Is This A Good Question/Topic? 0
  • +

Replies To: how to use Save dialog in my program?

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 05:32 AM

You can use the .InitialDirectory property. For example this is the My Documents folder -

saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments); 

Was This Post Helpful? 0
  • +
  • -

#3 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 05:40 AM

ok thanks so I have the following coding

        Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
        SaveFileDialog.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments)



but it says

Error	1	Reference to a non-shared member requires an object reference.	



I'm using VS 2010.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 05:42 AM

You have to use your object reference 'filesaveas'.
Was This Post Helpful? 0
  • +
  • -

#5 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 05:47 AM

This works too.

filesaveas.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

Was This Post Helpful? 0
  • +
  • -

#6 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 06:02 AM

ok thanks,

but when I click on my command button it does nothing?

i thought it would bring up the save file dialog box?

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
        filesaveas.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    End Sub


Was This Post Helpful? 0
  • +
  • -

#7 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 06:07 AM

you have to use the .ShowDialog() method to bring up the SaveFileDialog. More info

This post has been edited by nK0de: 28 April 2012 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 11:20 AM

thanks but I'm still stuck, I have got to the following

     Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
        filesaveas.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        filesaveas.ShowDialog()
        If filesaveas.ShowDialog() = DialogResult.OK Then
            ' create blank txt file
        End If


Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 11:27 AM

You don't need to ShowDialog twice -

Dim filesaveas As New SaveFileDialog
   filesaveas.Filter = "Text Files|*.txt"
   filesaveas.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

   If filesaveas.ShowDialog() = DialogResult.OK Then
       ' create blank txt file
   End If


Was This Post Helpful? 0
  • +
  • -

#10 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 11:42 AM

Ok,

so I have it partially working, my only problem is I had to hardcode the path when it should be saving the path from the dialog?

can you help me with the coding please?

        Dim fileLoc As String = "D:\Test\sample1.txt"
        Dim fs As FileStream = Nothing
        Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
        filesaveas.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        If filesaveas.ShowDialog() = DialogResult.OK Then
            If (Not File.Exists(fileLoc)) Then
                fs = File.Create(fileLoc)
                Using fs
                End Using
            End If
        End If



The problem with the above coding is that it will create a D:\Test\sample1.txt file when it should only create the file I want to create?
Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 12:17 PM

You get the filepath that you chose in the dialog via the .FileName property
Was This Post Helpful? 0
  • +
  • -

#12 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-April 12

Re: how to use Save dialog in my program?

Posted 28 April 2012 - 12:52 PM

ok thanks, cant believe it was as simple as

 fs = File.Create(filesaveas.FileName)



now for the final part, I dont want the default location to be my documents, I want the default location to be say D;\Test so I tried the following

 Dim fs As FileStream = Nothing
        Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = "Text Files|*.txt"
        filesaveas.InitialDirectory = Environment.GetFolderPath("D:\test")
        If filesaveas.ShowDialog() = DialogResult.OK Then
            fs = File.Create(filesaveas.FileName)
            Using fs
            End Using
        End If



It breaks on the GetFolderPath line with Conversion from string "D:\test" to type 'Integer' is not valid.

please ignore as I have resolved it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1