I’m trying to create a context menu which will be able to add extra menu items, with an attached child menu, as required.
As it stands my code generates no errors and it will flow through the program as intended, but it doesn’t display anything on screen.
I thought I might be missing the link between the ContextMenuStrip and the component it attaches to so I had a look at how the code is auto generated in the design view… however I have no access to the “Controls” object to do Control.Add(this.whatever) and it appears not to be part of the using System... collection.
I have left my testing for this commented out below.
The SuspendLayout and ResumeLayout methods are another thing I looked into after reading that you should call them before and after making changes to UI components. I am not sure if I am correctly using them as they appeared to do nothing, I’m unsure when they are actually required to be used.
Would appreciate being pointed towards what ever I have missed out on, thanks 🙂
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;
}
}
Well that was actually quite simple. You just have to do it from a Form.