Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created July 2, 2020 11:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bjoerntx/c6c4b98a602ca631b1f096cfda369b3e to your computer and use it in GitHub Desktop.
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