如何创建从CommonDialog类派生的自定义用户控件

本文关键字:派生 自定义 控件 用户 CommonDialog 何创建 创建 | 更新日期: 2023-09-27 18:09:22

我想扩展FolderBrowserDialog,以便有选项包括所选文件夹的子文件夹(添加复选框设置为包含或不包含)。我发现我不能扩展基本的FolderBrowserDialog,因为它是一个密封类。

所以我认为最简单的解决方案是创建一个用户控件,这是来自CommonDialog(相同的类是FolderBrowserDialog),从标准FolderBrowserDialog复制代码,只是改变它一点点,所以它也会有"包括子文件夹"复选框。

但是当我从默认FolderBrowserDialog复制代码时,它给了我一个错误:

missing partial modifier on declaration of type [my_class_name] another partial declaration of this type exists c#

指向"[my_class_name]. designer .cs"文件。

namespace my_custom_folder_open
{
    // Summary:
    //     Prompts the user to select a folder. This class cannot be inherited.
    [DefaultEvent("HelpRequest")]
    [DefaultProperty("SelectedPath")]
    [Designer("System.Windows.Forms.Design.FolderBrowserDialogDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    public sealed class UserControl2 : CommonDialog
    {
        // Summary:
        //     Initializes a new instance of the System.Windows.Forms.FolderBrowserDialog
        //     class.
        public UserControl2();
        // Summary:
        //     Gets or sets the descriptive text displayed above the tree view control in
        //     the dialog box.
        //
        // Returns:
        //     The description to display. The default is an empty string ("").
        [Browsable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string Description { get; set; }
        //
        // Summary:
        //     Gets or sets the root folder where the browsing starts from.
        //
        // Returns:
        //     One of the System.Environment.SpecialFolder values. The default is Desktop.
        //
        // Exceptions:
        //   System.ComponentModel.InvalidEnumArgumentException:
        //     The value assigned is not one of the System.Environment.SpecialFolder values.
        [Browsable(true)]
        [Localizable(false)]
        public Environment.SpecialFolder RootFolder { get; set; }
        //
        // Summary:
        //     Gets or sets the path selected by the user.
        //
        // Returns:
        //     The path of the folder first selected in the dialog box or the last folder
        //     selected by the user. The default is an empty string ("").
        [Browsable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string SelectedPath { get; set; }
        //
        // Summary:
        //     Gets or sets a value indicating whether the New Folder button appears in
        //     the folder browser dialog box.
        //
        // Returns:
        //     true if the New Folder button is shown in the dialog box; otherwise, false.
        //     The default is true.
        [Browsable(true)]
        [DefaultValue(true)]
        [Localizable(false)]
        public bool ShowNewFolderButton { get; set; }
        // Summary:
        //     Occurs when the user clicks the Help button on the dialog box.
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public event EventHandler HelpRequest;
        // Summary:
        //     Resets properties to their default values.
        public override void Reset();
        protected override bool RunDialog(IntPtr hWndOwner);
    }
}

哪里可能有问题?

顺便说一句,我已经创建了项目作为Windows窗体控制库..

如何创建从CommonDialog类派生的自定义用户控件

你真是大错特错了。这些对话框是组件,而不是控件。它们是Windows内置对话框的非常薄的包装。这些对话框本身对。net一无所知,并且是用非托管代码编写的。它们是组件而不仅仅是普通类的唯一原因是允许您在表单中放置一个。在设计器中设置一些属性很有帮助。

也许术语"CommonDialog"是误导。微软称其为"通用"只是因为它们是GUI程序中常用的对话框。并鼓励使用内置的方式,这样每个程序都有一个非常相似的方式来打开一个文件。

从CommonDialog派生没有多大意义,因为不需要创建自定义对话框。因为Windows只有内置的,并且它们已经被各自的。net类包装了。您的计划将破坏本机FolderBrowserDialog所能做的事情。它不包括显示复选框。

Hans非常正确,您不能通过从CommonDialog派生一个新类来解决您的问题。您可以做的是在文件夹选择器模式下使用原始IFileDialog组件。您还需要使用IFileDialogCustomize来添加复选框。因为这只是COM,所以在。net中使用它非常简单。