Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Drawing.Design;
- using System.Windows.Forms;
- namespace Core.Windows.Forms {
- /// <summary>
- /// Custom impl of SelectedPathEditor
- /// Usages:
- /// [Editor(typeof(SelectedDirectoryEditor),typeof(UITypeEditor))]
- /// public string MyDirectory { get; set; }
- /// </summary>
- public class SelectedDirectoryEditor:UITypeEditor {
- public override object EditValue(ITypeDescriptorContext context,IServiceProvider provider,object value) {
- //uses standard folder browser dialog
- using(FolderBrowserDialog dialog = new FolderBrowserDialog()) {
- dialog.ShowNewFolderButton = true;
- //setup description
- if(context.PropertyDescriptor != null) {
- if(!context.PropertyDescriptor.Description.IsNullOrEmptyTrim())
- dialog.Description = context.PropertyDescriptor.Description;
- else if(context.PropertyDescriptor.DisplayName.IsNullOrEmptyTrim())
- dialog.Description = context.PropertyDescriptor.DisplayName;
- }
- if(dialog.Description.IsNullOrEmptyTrim())
- dialog.Description = "Select a folder";
- string initialDirectory = value == null ? "" : value.ToString();
- if(initialDirectory.IsNullOrEmptyTrim()) {
- dialog.RootFolder = Environment.SpecialFolder.MyDocuments;
- }
- else {
- dialog.SelectedPath = initialDirectory;
- }
- //used when inheriting this component
- this.InitializeDialog(dialog,value);
- //show dialog
- if(dialog.ShowDialog(Form.ActiveForm) != DialogResult.OK) {
- //return initial value
- return value;
- }
- return dialog.SelectedPath;
- }
- }
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
- return UITypeEditorEditStyle.Modal;
- }
- protected virtual void InitializeDialog(FolderBrowserDialog dialog,object value) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment