locked
Save File Dialog Box RRS feed

  • Question

  • I'm new to Visual Basic with no programming in my background. I've created a application without to many problems until now. I've looked around msdn but could not find any tutorial on how to create a "standard" save file dialog box. What I have found is some instruction with a lot of assumptions made such as I've done this before.
    Wednesday, June 17, 2009 12:50 PM

Answers

  • @ bendernut - so what's the verdict?  did you get something put together that works?  If so, close this thread by marking one or more posts as answers.  If not, follow up with more posts!



    Doug
    • Marked as answer by bendernut Friday, June 19, 2009 2:41 AM
    Friday, June 19, 2009 1:01 AM
  • Thanks guys. Works great for my application. Short, sweet, and to the point.
    • Marked as answer by bendernut Friday, June 19, 2009 3:04 AM
    Friday, June 19, 2009 3:04 AM

All replies

  • Being new to using VB.Net (and programming), you might not be aware that VB.Net already has a SaveFileDialog.  No need to reinvent the wheel.  Look for it in the ToolBox in the Dialogs section on the left side of the screen.

    Look through the links in this forum search for code and explanations on how to use it:

    http://social.msdn.microsoft.com/Search/en-US/?Refinement=112&query=SaveFileDialog&rq=meta:Search.MSForums.ForumID(0f60fa48-1ceb-41ee-a10a-0dfcee7e19bd)&rn=Visual+Basic+General+Forum



    Doug
    Wednesday, June 17, 2009 1:13 PM
  • Here a short sample:

    Create a new Windows Form Application Project.
    Drag a RichTextBox1 control onto the form and a Button1.
    Make the RichTextBox1 box larger and place the Button underneath - looks better that way! :-)
    Double click the Button1 in the DESIGNER VIEW to switch to CODE VIEW and add a BUTTON1_CLICK event automatically.
    Insert the following code in the BUTTON1_CLICK event:

            'Create an instance of the SaveFileDialog:
            Dim SaveFile As New SaveFileDialog
    
            'Set a Filter for the SaveFileDialog, in this case *.rtf
            SaveFile.Filter = "RTF Document|*.rtf"
    
            'The title of the SaveFileDialog itself:
            SaveFile.Title = "Save an RTF File"
    
            'If a user clicks ok then proceed with saving the RTF Document:
            If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.RichText)
            End If

    Listing SPEC's is not a review !!! :-)
    • Edited by Earl Tut Wednesday, June 17, 2009 2:09 PM Code corrected, so, other users won't get it wrong! :-)
    Wednesday, June 17, 2009 1:23 PM
  • There's a bug in Earl Tut's sample code. After the SaveFileDialog is shown, you want to use it's FileName property to save the data in the file the user has selected in the dialog:

    If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
      RichTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.RichText)
    End If


    Another tip: You can drag the SaveFileDialog from the toolbox onto your form. It will show up in the components bar at the bottom. Then you can set the properties (like Filter and Title) in the designer.
    Mark the best replies as answers!
    Wednesday, June 17, 2009 2:01 PM
  • There's a bug in Earl Tut's sample code. After the SaveFileDialog is shown, you want to use it's FileName property to save the data in the file the user has selected in the dialog:

    If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
      RichTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.RichText)
    End If


    Another tip: You can drag the SaveFileDialog from the toolbox onto your form. It will show up in the components bar at the bottom. Then you can set the properties (like Filter and Title) in the designer.
    Mark the best replies as answers!

    Ups, ... sorry! I was soo much in a hurry typing down the simple code sample that I didn't even pay attention! I usually use OpenDileDialog more than SaveFileDialog - sorry for the misup - here the corrected entire code sample:

            'Create an instance of the SaveFileDialog:
            Dim SaveFile As New SaveFileDialog
    
            'Set a Filter for the SaveFileDialog, in this case *.rtf
            SaveFile.Filter = "RTF Document|*.rtf"
    
            'The title of the SaveFileDialog itself:
            SaveFile.Title = "Save an RTF File"
    
            'If a user clicks ok then proceed with saving the RTF Document:
            If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.SaveFile(SaveFile.FileName, RichTextBoxStreamType.RichText)
            End If

    Listing SPEC's is not a review !!! :-)
    Wednesday, June 17, 2009 2:07 PM
  • Another tip: You can drag the SaveFileDialog from the toolbox onto your form. It will show up in the components bar at the bottom. Then you can set the properties (like Filter and Title) in the designer.
    Mark the best replies as answers!

    In my post, I told where to find the SaveFileDialog control.  I'm just curious....do my responses not show up?


    Doug
    Thursday, June 18, 2009 1:53 AM
  • Another tip: You can drag the SaveFileDialog from the toolbox onto your form. It will show up in the components bar at the bottom. Then you can set the properties (like Filter and Title) in the designer.
    Mark the best replies as answers!

    In my post, I told where to find the SaveFileDialog control.  I'm just curious....do my responses not show up?


    Doug

    Sorry, I must have missed that. My response was also referring to Earl Tut's example, who created the dialog in code.
    Mark the best replies as answers!
    Thursday, June 18, 2009 10:44 AM
  • No problem.  I really was just curious whether my post showed up to anyone but me.   :)


    Doug
    Thursday, June 18, 2009 10:59 AM
  • @ bendernut - so what's the verdict?  did you get something put together that works?  If so, close this thread by marking one or more posts as answers.  If not, follow up with more posts!



    Doug
    • Marked as answer by bendernut Friday, June 19, 2009 2:41 AM
    Friday, June 19, 2009 1:01 AM
  • Thanks guys. Works great for my application. Short, sweet, and to the point.
    • Marked as answer by bendernut Friday, June 19, 2009 3:04 AM
    Friday, June 19, 2009 3:04 AM