I encountered something particularly weird when writing the overloaded Add methods. See the "Custom Filters" and "System File Types" sections above in this article for more information. In this last example above, I am adding six file types, with the first three being custom (made up by the programmer), and the last three being known file types obtained from the registry. FilterIndex = 1 ' Set the default extension ' Sets the filter index to the first item Let's take the example below:įilterBuild.Add( " Custom Files", " custm")įilterBuild.Add( " My Random Files", " rndm") One of these Add methods accepts two String parameters.
Read on to find out how to add a custom filter in just one line of code.Īs you have probably understood by now, filters are added through the overloaded Add method. This is especially useful if, like me, you use your own file types in your applications. Custom FiltersĪ great feature of FilterBuilder is that not only does it allow you to use any combination of the preset filters, but it also allows you to include custom filters. To get the associated system image for the file type, simply call the GetSystemFileTypeIcon method - passing in either a preset filter (from the Filters enum) or the string extension of the file type. It is either a 16x16px or a 32x32px icon. To change the size of the icon returned, you can modify the FileTypeIconSize property. PicIconView.Image = icon.ToBitmap() Extra Info ' Convert icon to bitmap and set as image ' Get the icon for XML files Dim icon As = _ ' Declarations Dim FilterBuild As New FilterBuilder()ĭim picIconView As New () What on earth is a FilterGroup? Well I do not know if there is an official term for something that for instance looks like: The finished result is a filter string ready for use in Dialog Boxes! FilterGroups After this, the resulting string is trimmed of blank spaces (" ") and " |" characters at either end. The ToString method uses a For Each loop to iterate over all the current items in the List(Of String) and concatenates each item with a " |" character. As mentioned in the "Using The Code" section, this is done through the ToString method. The great part is actually getting the required filter string - for use in Open and Save File Dialogs. If two string values are passed in, the filter string is built in the associated Add method, and added to the List. If an enum value is passed to the Add method (see the Examples section for more information), that value is converted into a string value through the private ReturnFilterAsString method and then added to the List. I thought that List(Of T) exposed many more methods and properties than what FilterBuilder truly needed to operate.Īll filters added through the Add method are added to the internal List, and conversely, filters removed through the Remove method are removed from the List. No doubt, the second example is much easier to use and also establishes consistency between different dialog boxes.įilterBuilder uses a List(Of String) and a StringBuilder to keep track of the filters, and to return the complete filter string.įor the sake of simplicity, I chose not to make FilterBuilder inherit directly from List(Of T) but instead use it internally. This is located in the Bin/Release folder.įilterBuild.Add()
To use the code, first add a reference to the FB.dll library. The utility had to be able to generate a completed filter string.The utility had to be able to add/remove filters easily.The utility had to also accept custom filters.
The utility had to have many preset filters.I needed a quick, easy way to add filters to the standard Open and Save File dialogs. I wrote FilterBuilder because I did not like writing lengthy filter strings for every Open/Save dialog that I used. As well as containing 135 of the most common file filters, FilterBuilder allows you to create endless combinations of file filters without ever having to maintain lengthy filter strings. FilterBuilder is a simple utility allowing you to easily create dialog filters.