andrew4582

SelectedDirectoryEditor

Sep 6th, 2010
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing.Design;
  4. using System.Windows.Forms;
  5.  
  6. namespace Core.Windows.Forms {
  7.  
  8.     /// <summary>
  9.     /// Custom impl of SelectedPathEditor
  10.     /// Usages:
  11.     /// [Editor(typeof(SelectedDirectoryEditor),typeof(UITypeEditor))]
  12.     /// public string MyDirectory { get; set; }
  13.     /// </summary>
  14.     public class SelectedDirectoryEditor:UITypeEditor {
  15.        
  16.         public override object EditValue(ITypeDescriptorContext context,IServiceProvider provider,object value) {
  17.             //uses standard folder browser dialog
  18.             using(FolderBrowserDialog dialog = new FolderBrowserDialog()) {
  19.                
  20.                 dialog.ShowNewFolderButton = true;
  21.  
  22.                
  23.                 //setup description
  24.                 if(context.PropertyDescriptor != null) {
  25.                     if(!context.PropertyDescriptor.Description.IsNullOrEmptyTrim())
  26.                         dialog.Description = context.PropertyDescriptor.Description;
  27.                     else if(context.PropertyDescriptor.DisplayName.IsNullOrEmptyTrim())
  28.                         dialog.Description = context.PropertyDescriptor.DisplayName;
  29.                 }
  30.  
  31.                 if(dialog.Description.IsNullOrEmptyTrim())
  32.                     dialog.Description = "Select a folder";
  33.  
  34.  
  35.                 string initialDirectory = value == null ? "" : value.ToString();
  36.  
  37.                 if(initialDirectory.IsNullOrEmptyTrim()) {
  38.                     dialog.RootFolder = Environment.SpecialFolder.MyDocuments;
  39.                 }
  40.                 else {
  41.                     dialog.SelectedPath = initialDirectory;
  42.                 }
  43.  
  44.                 //used when inheriting this component
  45.                 this.InitializeDialog(dialog,value);
  46.  
  47.                 //show dialog
  48.                 if(dialog.ShowDialog(Form.ActiveForm) != DialogResult.OK) {
  49.                    
  50.                     //return initial value
  51.                     return value;
  52.                 }
  53.                
  54.                 return dialog.SelectedPath;
  55.             }
  56.         }
  57.         public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
  58.             return UITypeEditorEditStyle.Modal;
  59.         }
  60.         protected virtual void InitializeDialog(FolderBrowserDialog dialog,object value) {
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment