Results 1 to 6 of 6

Thread: SaveFileDialog Problem

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Question SaveFileDialog Problem

    Hi guys,
    There is a wired problem that I am facing with SaveFileDialog box component. The problem is that when the user wants to save an image file and clicks on a file in the dialog box and then changes the format (extension) of the file to save the file with that extension the file name still contains the old file extension that previously was selected.

    This is a problem because the file is being saved with correct format but with different extension. I also set the OverwritePrompt to True but since the file name contains the old file extension it warns the user to overwrite a file which has an extension that is not selected by the user.

    I check all properties and did some tests but until now I can't fined a solution. Am I missing something here?
    Thanks in advance,
    VBDT
    Attached Images Attached Images  

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SaveFileDialog Problem

    Yes, its a bug that has plauged Windows forever. It does it with file names too. When ever you select anything in the dialog and try to type and chage it in the file textbox it will still retain the selected file.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: SaveFileDialog Problem

    Quote Originally Posted by RobDog888
    Yes, its a bug that has plauged Windows forever. It does it with file names too. When ever you select anything in the dialog and try to type and chage it in the file textbox it will still retain the selected file.
    Hi Rob and thanks for the response. It looks like the SaveFileDialog or FileDialog is not inheritable. Is there any work around to this problem?

  4. #4
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: SaveFileDialog Problem

    That's really strange and I've never heard of that happening. I'm not sure if this will fix it but I think it will:
    if SFD is your save file dialog box then:

    Code:
                        sfd.AddExtension = True
                        sfd.DefaultExt = ".jpg"
                        sfd.SupportMultiDottedExtensions = False
    I don't remember if that last one is supposed to be false or true but I think that would do it. Actually I'm only 90% sure that the addextension part is correct either lol. I'm very sure that some combination of those settings will prevent it from grabbing the extension at all in the first place from files they click on in there.
    I believe the way I wrote it, the second line will set the default extension to jpg or whatever you want. Then the first line will ensure that if the user doesn't add an extension, it won't save as an extensionless file (I think this is the default anyway). And the third line will prevent it from getting saved as picture.txt.jpg but I'm not sure if it errors out or strips out the period or strips off the jpg or the txt part. I don't remember, just mess with it.

    Either that or way way way way way better is to do sfd.AddExtension = False and sfd.SupportMultiDottedExtensions = False and then the dialog box returns the file path string they want sometimes with and sometimes without an extension. So then check if there's a period in the entire string at all. If there is, remove the extension. The best way to do this is to reverse the string and start from the last character which would now be first character and search for the index of the first period then first slash and if the slash is after the period, remove the period and every character before it. If the period is after the slash, they have a folder name with a period in it or something crazy like that. Then re-reverse it. That just stripped off the period and file extension that you'd get as a result of them clicking on a file. Then append the filepath string to end in whatever extension you want like
    sfd.FilePath &= ".jpg"
    actually it might not like that. You might have to store it in a string variable first.
    Last edited by Desolator144; Dec 28th, 2007 at 07:01 PM.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  5. #5

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: SaveFileDialog Problem

    Hi Desolator144, actually I found a lot about this problem on the net and it shows that this is a well known bug in windows. Unfortunately setting all the properties you showed doesn’t solve the problem. Fortunately the dialog box’s OK event handler has a Cancel property that I will see if I can use it. If this property prevents the SaveFileDialog box to close than I may be able to show my own Overwrite Prompt and change the extension manually.

  6. #6

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: SaveFileDialog Problem

    I found a work around about this bug using the Cancel property of the FileOk event handler argument. Here is the code in the event; you must set the “OverwritePromt” property of the SaveFileDialog box to False.

    vb Code:
    1. Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
    2.         Dim fileName As String = Me.SaveFileDialog1.FileName
    3.         Dim ext As string
    4.         Select Case Me.SaveFileDialog1.FilterIndex
    5.             Case 1
    6.                 ext = "png"
    7.             Case 2
    8.                 ext = "jpeg"
    9.             Case 3
    10.                 ext = "bmp"
    11.             Case 4
    12.                 ext = "gif"
    13.             Case 5
    14.                 ext = "tiff"
    15.             Case 6
    16.                 ext = "emf"
    17.             Case 7
    18.                 ext = "wmf"
    19.         End Select
    20.         fileName = IO.Path.ChangeExtension(fileName, ext )
    21.         Me.SaveFileDialog1.FileName = fileName
    22.         If IO.File.Exists(fileName) Then
    23.             If MessageBox.Show("File " & fileName & " already exists." & Environment.NewLine & _
    24.             "Do you want to replace it?", Me.SaveFileDialog1.Title, MessageBoxButtons.YesNo, _
    25.             MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
    26.                 e.Cancel = True
    27.             End If
    28.         End If
    29.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width