2012-01-05 55 views
0

我正嘗試創建一個上下文菜單,該菜單可以根據需要添加額外的菜單項,並附加子菜單。非自動生成的ContextMenu不會顯示

因爲它代表我的代碼不會產生錯誤,它將按照預期流過程序,但它不會在屏幕上顯示任何內容。

我想我可能會錯過ContextMenuStrip和它附加的組件之間的鏈接,所以我看看代碼是如何在設計視圖中自動生成的......但是我無法訪問「控件」對象做Control.Add(this.whatever),它似乎不是using System...集合的一部分。 我已經將我的測試留在下面評論了。

SuspendLayout和ResumeLayout方法是我在閱讀後研究的另一件事情,即在對UI組件進行更改之前和之後應調用它們。我不確定我是否正確使用它們,因爲它們看起來什麼都不做,我不確定它們何時需要使用。

希望瞭解正在朝着指向什麼都我已經錯過了,謝謝:)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
using System.IO; 

namespace Context 
{ 
class PopulateMenu 
{ 
    private void PopulateSubMenu(string fileName, ContextMenuStrip parent, EventHandler onClickDelete, EventHandler onClickView) 
    { 
     try 
     { 
      Image delete = Properties.Resources.RO_Mx2_24_delete; 
      Image view = Properties.Resources.OpenFolder1; 

      parent.SuspendLayout(); 
      //Parent 
      ToolStripMenuItem attachedFiles = new ToolStripMenuItem(fileName, Properties.Resources.NewDocumentHS); 
      //Kids 
      attachedFiles.DropDownItems.Add(new ToolStripMenuItem("View File", view, onClickView)); 

      //if (!hotfolder) 
      //{ 
      attachedFiles.DropDownItems.Add(new ToolStripMenuItem("Delete File", delete, onClickDelete)); 
      //} 
      //else 
      //{ 
      // attachedFiles.DropDownItems.Add(new ToolStripMenuItem("Remove File", delete, onClickDelete)); 
      //} 

      // Attach kid to parent 
      parent.Items.Add(attachedFiles); 
      parent.ResumeLayout(); 
     } 
     catch { Exception e; } 
    } 


    private void BuildHotMenu(List<FileInfo> files, ContextMenuStrip parent) 
    { 
     try 
     { 
      parent.SuspendLayout(); 
      parent.Items.Add(new ToolStripMenuItem("Hot Folder Files")); 
      parent.Items.Add(new ToolStripSeparator()); 

      foreach (FileInfo fileInfo in files) 
      { 
       PopulateSubMenu(fileInfo.Name, parent, null, null); 
      } 
      parent.ResumeLayout(); 
     } 
     catch { Exception e; } 
    } 

    private void BuildDropMenu(List<FileInfo> files, ContextMenuStrip parent) 
    { 
     try 
     { 
      parent.SuspendLayout(); 
      parent.Items.Add(new ToolStripMenuItem("Dropped Files")); 
      parent.Items.Add(new ToolStripSeparator()); 

      foreach (FileInfo fileInfo in files) 
      { 
       PopulateSubMenu(fileInfo.Name, parent, null, null); 
      } 
      parent.ResumeLayout(); 
     } 
     catch { Exception e; } 
    } 

    public void SummonContextMenu() 
    { 
     try 
     { 
      ContextMenuStrip attachedFilesWithMenu = new ContextMenuStrip(); 
      ////ContextMenu 
      //attachedFilesWithMenu.Name = "attachFilesWithMenu"; 
      //attachedFilesWithMenu.Size = new Size(61, 4); 

      //ToolStrip ToolStrip1 = new ToolStrip(); 
      ////ToolStrip 
      //toolStrip1.ContextMenuStrip = attachedFilesWithMenu; 
      //toolStrip1.Location = new Point(0, 0); 
      //toolStrip1.Size = new Size(284, 25); 
      //toolStrip1.Name = "toolStrip1"; 
      //toolStrip1.Text = "toolStrip1"; 

      //// Form2 
      //// 
      //AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      //AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      //ClientSize = new System.Drawing.Size(284, 262); 
      //Controls.Add(this.toolStrip1); 
      //ResumeLayout(false); 
      //PerformLayout(); 




      //static directory for testing only 
      ConfigurationInformation configurationInformation = new ConfigurationInformation(); 
      configurationInformation.HotFolderPath = "C:\\Users\\alexh\\HotFolder\\"; 

      //Store DirectoryInfo 
      DirectoryInfo dirInfo = new DirectoryInfo(Environment.ExpandEnvironmentVariables(configurationInformation.HotFolderPath)); 
      //Create list to store file details 
      List<FileInfo> hotFiles = new List<FileInfo>(); 

      foreach (FileInfo fileInfo in dirInfo.GetFiles()) 
      { 
       hotFiles.Add(fileInfo); 
      } 
      BuildHotMenu(hotFiles, attachedFilesWithMenu); 
      BuildDropMenu(hotFiles, attachedFilesWithMenu); 
     } 
     catch { Exception e; } 
    } 

    public PopulateMenu() 
    { 
    } 
    //private System.Windows.Forms.ToolStrip toolStrip1; 
    //private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; 
} 

}

+0

那麼這其實很簡單。你只需要從表格中完成。 //this.Literally = theAnswer; this.ContextMenuStrip = myContextMenu; – negligible 2012-01-05 23:11:42

回答

0

嗯,這實際上是相當簡單的。你只需要從表格中完成。

//this.Literally = theAnswer; 
this.ContextMenuStrip = myContextMenu;