Skip to content

Instantly share code, notes, and snippets.

@michaeljbailey
Created April 13, 2015 18:39
Show Gist options
  • Save michaeljbailey/15a6852f1160b1961a3e to your computer and use it in GitHub Desktop.
Save michaeljbailey/15a6852f1160b1961a3e to your computer and use it in GitHub Desktop.
Builds up the filter for a file dialog.
public class FileDialogFilter
{
private readonly string _filter;
private readonly string _description;
public string Description
{
get { return _description; }
}
public string Filter
{
get { return _filter; }
}
public FileDialogFilter(string description, string filter)
{
_description = description;
_filter = filter;
}
public static string From(params string[] values)
{
if (values.Length % 2 == 1)
{
throw new ArgumentException(@"Must have a matching filter for each description", "values");
}
var filters = new FileDialogFilter[values.Length / 2];
for (var i = 0; i < filters.Length; i++)
{
filters[i] = new FileDialogFilter(values[2 * i], values[2 * i + 1]);
}
return filters.Build();
}
public override string ToString()
{
return string.Format("{0} ({1})|{1}", Description, Filter);
}
}
public static class FileDialogFilterExtensions
{
public static string Build(this IList<FileDialogFilter> filters)
{
return string.Join("|", filters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment