Created
July 2, 2020 11:08
-
-
Save bjoerntx/c6c4b98a602ca631b1f096cfda369b3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TextControlExtensions | |
{ | |
public static void CustomDialog ( | |
this TXTextControl.ImageCollection images, | |
string preferredFormat = null, | |
bool showAll = true, | |
OpenFileDialog openFileDialog = null) | |
{ | |
// create a new open file dialog or use the given one | |
OpenFileDialog dlg = (openFileDialog == null) | |
? new OpenFileDialog() : openFileDialog; | |
// retrieve supported filters from Text Control | |
string sImportFormats = images.ImportFilters; | |
// add an "All Supported Formats" entry with all extensions | |
if (showAll) { | |
var sAllImportFormats = String.Join(";", | |
images.ImportFilters.Split('|') | |
.Where((value, index) => index % 2 == 1) | |
.ToArray()); | |
sImportFormats = "All Supported Formats|" + | |
sAllImportFormats + "|" + sImportFormats; | |
} | |
// set the filters for the dialog | |
dlg.Filter = sImportFormats; | |
// select the pre-selected filter | |
if (preferredFormat != null) { | |
var saFinalFilters = dlg.Filter.Split('|').Where( | |
(value, index) => index % 2 == 1).ToArray(); | |
// set the index (0-based) | |
dlg.FilterIndex = Array.FindIndex(saFinalFilters, m => m == preferredFormat) + 1; | |
} | |
// if file is opened by user, create a new image and insert it | |
if (dlg.ShowDialog() == true) { | |
TXTextControl.Image image = new TXTextControl.Image( | |
System.Drawing.Image.FromFile(dlg.FileName)); | |
images.Add(image, -1); // at input position | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment