コード例 #1
0
        /// <summary>
        /// See base class docs.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var result = value as string ?? "";

            string folder = null;
            if(!String.IsNullOrEmpty(result)) folder = Path.GetDirectoryName(result);
            if(!Directory.Exists(folder)) folder = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            var fileNameBrowserAttribute = context.PropertyDescriptor.Attributes.OfType<FileNameBrowserAttribute>().FirstOrDefault();
            if(fileNameBrowserAttribute == null) fileNameBrowserAttribute = new FileNameBrowserAttribute();

            using(var dialog = new OpenFileDialog() {
                AddExtension = fileNameBrowserAttribute.AddExtension,
                AutoUpgradeEnabled = true,
                CheckFileExists = fileNameBrowserAttribute.CheckFileExists,
                DefaultExt = fileNameBrowserAttribute.DefaultExtension,
                FileName = result,
                Filter = fileNameBrowserAttribute.Filter,
                InitialDirectory = folder,
                Multiselect = false,
                Title = String.IsNullOrEmpty(fileNameBrowserAttribute.BrowserTitle) ? "" : Localise.GetLocalisedText(fileNameBrowserAttribute.BrowserTitle),
            }) {
                if(dialog.ShowDialog() == DialogResult.OK) result = dialog.FileName;
            }

            return result;
        }
コード例 #2
0
        /// <summary>
        /// See base class docs.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var result = value as string ?? "";

            string folder = null;

            if (!String.IsNullOrEmpty(result))
            {
                folder = Path.GetDirectoryName(result);
            }
            if (!Directory.Exists(folder))
            {
                folder = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            }

            var fileNameBrowserAttribute = context.PropertyDescriptor.Attributes.OfType <FileNameBrowserAttribute>().FirstOrDefault();

            if (fileNameBrowserAttribute == null)
            {
                fileNameBrowserAttribute = new FileNameBrowserAttribute();
            }

            using (var dialog = new OpenFileDialog()
            {
                AddExtension = fileNameBrowserAttribute.AddExtension,
                AutoUpgradeEnabled = true,
                CheckFileExists = fileNameBrowserAttribute.CheckFileExists,
                DefaultExt = fileNameBrowserAttribute.DefaultExtension,
                FileName = result,
                Filter = fileNameBrowserAttribute.Filter,
                InitialDirectory = folder,
                Multiselect = false,
                Title = String.IsNullOrEmpty(fileNameBrowserAttribute.BrowserTitle) ? "" : Localise.GetLocalisedText(fileNameBrowserAttribute.BrowserTitle),
            }) {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    result = dialog.FileName;
                }
            }

            return(result);
        }