繁体   English   中英

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

[英]How to create custom User Control derived from CommonDialog class

我想扩展FolderBrowserDialog以选择包含选定文件夹的子文件夹(添加复选框以设置为包含或不包含)。 我发现我无法扩展基本的FolderBrowserDialog,因为它是密封类。

因此,我认为最简单的解决方案是创建一个从CommonDialog(与FolderBrowserDialog相同的类)派生的用户控件,从标准FolderBrowserDialog复制代码,然后对其稍作改动,使其具有“ Include Subfolders” ”复选框。

但是,当我从默认的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窗体控件库。

您在此方面正处于错误的轨道。 这些对话框是组件,而不是控件。 它们是Windows内置对话框的非常薄的包装。 这些对话框本身对.NET一无所知,并以非托管代码编写。 它们是Component而不是普通类的唯一原因是允许您在表单上放置一个。 与设计师一起设置一些属性很有帮助。

也许“ CommonDialog”一词具有误导性。 Microsoft之所以称它为“普通”,仅是因为它们是GUI程序中的常用对话框。 并鼓励使用内置程序,以便每个程序都具有与打开文件非常相似的方式。

从CommonDialog派生没有太大意义,因为不需要创建自定义对话框。 因为Windows仅具有内置的Windows,并且它们已经由各自的.NET类包装。 您的计划将破坏本机FolderBrowserDialog的功能。 其中不包括显示复选框。 它被密封是有充分的理由的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM