2012-01-05 13 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

答えて

0

まあ、を逃してきたこれまでどのような方に指摘されてお願い申し上げます。フォームからそれを行うだけです。

//this.Literally = theAnswer; 
this.ContextMenuStrip = myContextMenu; 
関連する問題