Results 1 to 4 of 4

Thread: Please help modify code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Please help modify code

    I learn OpenFileDialog but I don't know how to remove "All Files", "*.*" from OpenFileDialog.
    I want to list only Excel, PDF, Word and Jpg files.
    Please help.

    Dim fdlg As OpenFileDialog = New OpenFileDialog
    fdlg.Title = "Select file"
    fdlg.InitialDirectory = Environment.SpecialFolder.Desktop.ToString

    fdlg.Filter = String.Format("{0}{1}{2} ({3})|{3}", fdlg.Filter, "", "All Files", "*.*")

    fdlg.Filter = (fdlg.Filter + "|Excel Files (.xls ,.xlsx)| *.xls ;*.xlsx")
    fdlg.Filter = (fdlg.Filter + "|PDF Files (.pdf)|*.pdf")
    fdlg.Filter = (fdlg.Filter + "|Word Files (.docx ,.doc)|*.docx;*.doc")
    fdlg.Filter = (fdlg.Filter + "|Jpg Files (*.jpg)|*.jpg")

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Please help modify code

    If you don't want it listed, then remove the line of code that is adding it. You may need to adjust a subsequent line of code slightly because it seems to assume that the fdlg.Filter already contains something by the time that line is hit, but this should be a very straightforward change to make as a whole.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Please help modify code

    Here's how...

    Code:
    Dim fdlg As OpenFileDialog = New OpenFileDialog
    fdlg.Title = "Select file"
    fdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    fdlg.Filter = "Excel Files (.xls ,.xlsx)| *.xls ;*.xlsx|PDF Files (.pdf)|*.pdf|Word Files (.docx ,.doc)|*.docx;*.doc|Jpg Files (*.jpg)|*.jpg"

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Please help modify code

    I have written this into my code library to do this, it reads the file descriptions from the registry .. so you can just go:

    VB.Net Code:
    1. ofd.Filter = {"xls", "xlsx", "pdf", "docx", "doc", "jpg"}.ToFileDialogFilter(True, , , False)
    2. 'Parameters:
    3. '    Exentsions
    4. '    ShowAllSupported - Shows "All Supported Files" option with each of the extensions also available in a single item
    5. '    SupportedFileText - "All Supported Files" Text (if ShowAllSupported = True)
    6. '    ShowExtensions - Show the user file extensions or not after each setting - if Nothing this uses the windows setting (Is not respected in some versions of Windows)
    7. '    ShowAllFiles - Shows an option to show all files *.*

    This sets the filter to:
    Code:
    All Supported Files (*.xls;*.xlsx;*.pdf;*.docx;*.doc;*.jpg)|*.xls;*.xlsx;*.pdf;*.docx;*.doc;*.jpg|Microsoft Excel 97-2003 Worksheet (*.xls)|*.xls|Microsoft Excel Worksheet (*.xlsx)|*.xlsx|.pdf file (*.pdf)|*.pdf|Microsoft Word Document (*.docx)|*.docx|Microsoft Word 97 - 2003 Document (*.doc)|*.doc|JPEG Image (*.jpg)|*.jpg
    PDF doesn't have a description on mine because I don't have any specific PDF app installed .

    Lib code is as follows:

    VB.Net Code:
    1. 'Extracted from i00CodeLib - Do not remove this message
    2. Public Module Extensions
    3.     <System.Runtime.CompilerServices.Extension>
    4.     Public Function ToFileDialogFilter(Exentsions As String(), ShowAllSupported As Boolean, Optional SupportedFileText As String = "All Supported Files", Optional ShowExtensions As Boolean? = Nothing, Optional ShowAllFiles As Boolean? = Nothing) As String
    5.         Dim FileExt = {New With {.Ext = "", .Desc = ""}}.ToList
    6.         FileExt.Clear()
    7.  
    8.         'IN SOME VERSIONS OF WINDOWS THE OS CAN OVERRIDE THE ShowExtensions SETTING!
    9.         If ShowExtensions.HasValue = False Then ShowExtensions = Shell.OS.ShowingFileExtensions
    10.         If ShowAllFiles.HasValue = False Then ShowAllFiles = ShowAllSupported
    11.  
    12.         Dim Extensions As New List(Of String)
    13.         For Each ExtensionGroup In Exentsions
    14.             Dim arrExtensions = Split(ExtensionGroup, ";")
    15.             Dim NiceExtensions = arrExtensions.Select(Function(x) "*." & x).ToArray
    16.             Extensions.AddRange(NiceExtensions)
    17.             Dim ExtensionFilter = Join(NiceExtensions, ";")
    18.             FileExt.Add(New With {.Ext = ExtensionFilter,
    19.                                   .Desc = Replace(Shell.FileInfo.GetFileTypeDescription(arrExtensions(0), True), "|", "")})
    20.         Next
    21.  
    22.         If Extensions.Count = 1 Then
    23.             'don't need all supported as only has one type!
    24.         ElseIf ShowAllSupported Then
    25.             Dim AllSupported = New With {.Ext = Join(Extensions.ToArray, ";"),
    26.                                          .Desc = SupportedFileText}
    27.             FileExt.Insert(0, AllSupported)
    28.         End If
    29.         If ShowAllFiles.Value Then
    30.             FileExt.Add(New With {.Ext = "*.*",
    31.                                   .Desc = "All Files"})
    32.         End If
    33.         Return Join(FileExt.Select(Function(x) x.Desc & If(ShowExtensions.Value, " (" & x.Ext & ")", "") & "|" & x.Ext).ToArray, "|")
    34.     End Function
    35. End Module
    36.  
    37. Namespace Shell
    38.  
    39.     Partial Public Class OS
    40.  
    41.         Public Shared ReadOnly Property ShowingFileExtensions As Boolean
    42.             Get
    43.                 Static mc_ShowingFileExtensions As Boolean
    44.                 Static mc_Obtained As Boolean
    45.                 If mc_Obtained = False Then
    46.                     mc_Obtained = True
    47.                     Try
    48.                         Dim regKey = My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced")
    49.                         If regKey IsNot Nothing Then
    50.                             mc_ShowingFileExtensions = Not CBool(regKey.GetValue("HideFileExt"))
    51.                         End If
    52.                     Catch ex As Exception
    53.                         'key permission issue?? - assume false
    54.                     End Try
    55.                 End If
    56.                 Return mc_ShowingFileExtensions
    57.             End Get
    58.         End Property
    59.  
    60.     End Class
    61.  
    62.     Public Class FileInfo
    63.         Public Shared Function GetFileTypeDescription(Extension As String, Optional FillIfEmpty As Boolean = True) As String
    64.             If Extension Is Nothing Then Return Nothing
    65.             Extension = Extension.TrimStart("."c)
    66.             Using extKey = My.Computer.Registry.ClassesRoot.OpenSubKey("." & Extension)
    67.                 If extKey IsNot Nothing Then
    68.                     Dim extClass = TryCast(extKey.GetValue(""), String)
    69.                     If extClass <> "" Then
    70.                         Using extClassKey = My.Computer.Registry.ClassesRoot.OpenSubKey(extClass)
    71.                             If extClassKey IsNot Nothing Then
    72.                                 Dim ExtensionDesc = TryCast(extClassKey.GetValue(""), String)
    73.                                 If ExtensionDesc <> "" Then
    74.                                     Return ExtensionDesc
    75.                                 End If
    76.                             End If
    77.                         End Using
    78.                     End If
    79.                 End If
    80.             End Using
    81.             If FillIfEmpty AndAlso Extension <> "" Then
    82.                 Return "." & Extension & " file"
    83.             Else
    84.                 Return ""
    85.             End If
    86.         End Function
    87.     End Class
    88.  
    89. End Namespace

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