using System; using System.IO; using System.Globalization; using System.Drawing.Design; using System.Windows.Forms; using System.Windows.Forms.Design; using System.ComponentModel; namespace Orthogonal.Common { /// /// This class extends the to popup an /// when selected in the control. NOTE: This class is redundant, /// as there is a standard class somewhere in the Framework to perform the identical task. /// public class FilePickUITypeEditor : UITypeEditor { //========================================================================================== public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } //========================================================================================== public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if( edSvc == null ) return null; // There is no need for edSvc.ShowDialog(form) in this case as we're simply // showing a file pick dialog and return the result. No custom form is needed. OpenFileDialog dlg = new OpenFileDialog(); dlg.CheckFileExists = true; dlg.FilterIndex = 1; dlg.RestoreDirectory = true; dlg.Title = "Select the file for the property"; if ((value != null) && (value is string)) { string val = value.ToString(); if (Path.IsPathRooted(val)) dlg.InitialDirectory = Path.GetDirectoryName(val); else dlg.InitialDirectory = Environment.CurrentDirectory; if (Path.HasExtension(val)) { string rawext = Path.GetExtension(val).Substring(1); dlg.Filter = string.Format("{0} files (*.{1})|*.{1}|All files (*.*)|*.*", rawext.ToUpper(CultureInfo.CurrentCulture), rawext); } else { dlg.Filter = "All files (*.*)|*.*"; } } if (dlg.ShowDialog() == DialogResult.OK) { return dlg.FileName; } return base.EditValue (context, provider, value); } } }