2016-06-25 30 views
1

我有一個工具欄,其中包含表示在目錄中找到的文件夾的toolstripdropdownbuttons。我希望每個工具條下拉菜單的下拉菜單包含找到的子文件夾。這方面的一個例子是Internet Explorer的鏈接欄我曾嘗試下面的代碼,但我不太清楚如何去做(見圖片) Links Bar Example如何使用下拉菜單(C#Winforms)以編程方式將ToolstripDropDownButton添加到工具欄中

碼我曾嘗試:

 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); 

     } 

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

我該如何做到這一點?

回答

0

您需要通過ToolStripDropDownButton,然後使用DropDownItems屬性添加子項目。

populateLinksFolders(linksDirectory, button); 

然後:

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); 
    } 
} 
+0

感謝這個回答我queston – LordPerun

+0

一個有趣的問題是約在下拉菜單太小了,有沒有辦法自動大小,或解決這一問題? – LordPerun

+0

如果這回答您的問題,請考慮使用答案旁邊的複選標記。該按鈕應該默認爲自動調整大小,但如果沒有,您可以添加'button.AutoSize = true;'。 – endofzero

相關問題