Howto create a "Save File" Dialog using VB.NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Howto create a "Save File" Dialog using VB.NET

  1. #1
    Michael Stettner Guest

    Howto create a "Save File" Dialog using VB.NET


    Hello,

    another newbie question:

    what is the (elegant) way to create a standard "Save File" Dialog using
    VB.NET

    Greetings

    Michael


  2. #2
    Mattias Sjögren Guest

    Re: Howto create a "Save File" Dialog using VB.NET

    Michael,

    >what is the (elegant) way to create a standard "Save File" Dialog using
    >VB.NET


    Use the System.Windows.Forms.SaveFileDialog class. If you're using
    VS.NET, you shoud be able to find it in the toolbox. Just set its
    properties, then call ShowDialog at runtime.


    Mattias

    ===
    Mattias Sjögren (VB MVP)
    mattias @ mvps.org
    http://www.msjogren.net/dotnet/

  3. #3
    Join Date
    Jan 2005
    Location
    New Zealand
    Posts
    319
    Hi Mattias,
    Can you show me the sample VB.Net 2003 SaveFileDialog please.

    Thanks.
    Cheers,
    Lennie

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    This example came from the Visual Studio 2005 Express Help File, but I don't think it has changed much from 2003.
    Code:
    The following code example illustrates creating a SaveFileDialog, setting members, 
    calling the dialog box using the ShowDialog method, and saving the current file. 
    The example requires a form with a button placed on it. 
    
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()
    
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
    
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If
    End Sub

  5. #5
    Join Date
    Jan 2005
    Location
    New Zealand
    Posts
    319
    Hi Ron,
    Thanks for your sample script. I have tried it and it's working now. You are just awesome.

    Have a good day.
    Cheers,
    Lennie

  6. #6
    Join Date
    Jan 2010
    Posts
    2
    Sorry to reopen an age-old thread, but I've struggled with this for a few hours:

    How do you write to the stream once you've created it?

    myStream = saveFileDialog1.OpenFile()
    If (myStream IsNot Nothing) Then
    ' Code to write the stream goes here.
    myStream.Close()
    End If

    Aka: what goes in "Code to write the stream?" I have a textbox, the text inside of which I'd like to write to a CSV file. Everything is working except for actually writing to the stream. Can someone help me with this?

    Thanks in advance

  7. #7
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    You could use the StreamWriter Class, Here is a simple example added to the original code:
    Code:
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()
    
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
    
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
    
            Using sw As StreamWriter = New StreamWriter(myStream)
                ' Add some text to the file.
                sw.Write("This is the ")
                sw.WriteLine("header for the file.")
                sw.WriteLine("-------------------")
                ' Arbitrary objects can also be written to the file.
                sw.Write("The date is: ")
                sw.WriteLine(DateTime.Now)
                sw.Close()
            End Using
    
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If
    End Sub

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Or try something as simple as
    Code:
    System.IO.File.WriteAllText("c:\ohio.csv", TextBox1.Text)
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  9. #9
    Join Date
    Jan 2010
    Posts
    2
    Thanks a lot! that Using command did it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center