简体   繁体   English

如何以编程方式将ToolstripDropDownButton添加到带有下拉菜单的工具条(C#Winforms)

[英]How to Programmatically add a ToolstripDropDownButton to a toolstrip with dropdown menu (C# Winforms)

I have a toolstrip that will contain toolstripdropdownbuttons that represent folders that are found in a directory. 我有一个工具条,它将包含toolstripdropdownbuttons,它们代表目录中的文件夹。 I would like a dropdown menu to each toolstripdropdownbutton to contain subfolders that are found. 我想为每个toolstripdropdownbutton提供一个下拉菜单,以包含找到的子文件夹。 An example of this would be the Internet Explorer Links bar I have tried the following code but I'm not quite sure how to go about it (see picture) Links Bar Example 一个例子是Internet Explorer链接栏我尝试了以下代码,但我不太清楚如何去做(见图) 链接栏示例

Code I have tried: 代码我尝试过:

        private void populateLinks()
    {
        linksToolStrip.Items.Clear();
        DirectoryInfo linksFolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\\Links");
        foreach (DirectoryInfo linksDirectory in linksFolder.GetDirectories())
        {
            Image favImage = Properties.Resources.Folder;
            ToolStripDropDownButton button = new ToolStripDropDownButton();
            button.Text = Truncate(linksDirectory.Name, 22);
            button.ToolTipText = linksDirectory.Name + "\nDate Created: " + Directory.GetCreationTime(linksDirectory.FullName);
            button.Image = favImage;
            button.Tag = linksDirectory.FullName;
            linksToolStrip.Items.Add(button);
            populateLinksFolders(linksDirectory, button.DropDown);

        }

and

private void populateLinksFolders(DirectoryInfo subdirectory, ToolStripDropDown tsdd)
    {
        foreach (DirectoryInfo directory in subdirectory.GetDirectories())
        {
            populateLinksFolders(directory, ?) //<- Everything tried here fails
        }
    }

How can I accomplish this? 我怎么能做到这一点?

You need to pass the ToolStripDropDownButton , then use the DropDownItems property to add subitems. 您需要传递ToolStripDropDownButton ,然后使用DropDownItems属性添加子项。

populateLinksFolders(linksDirectory, button);

Then: 然后:

private void populateLinksFolders(DirectoryInfo subdirectory, ToolStripDropDownButton tsddb)
{
    foreach (DirectoryInfo directory in subdirectory.GetDirectories())
    {
        ToolStripDropDownButton button = new ToolStripDropDownButton(directory.Name);
        tsddb.DropDownItems.Add(button);
        populateLinksFolders(directory, button);
    }
}

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

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