非自动生成的ContextMenu将不会显示

时间:2012-01-05 17:27:23

标签: c# winforms contextmenu

我正在尝试创建一个上下文菜单,可以根据需要添加额外的菜单项和附加的子菜单。

现在我的代码没有产生任何错误,它会按预期流过程序,但它不会在屏幕上显示任何内容。

我以为我可能会错过ContextMenuStrip与它附加的组件之间的链接,所以我看看代码是如何在设计视图中自动生成的...但是我无法访问“Controls”对象要做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;
}

}

1 个答案:

答案 0 :(得分:0)

那实际上非常简单。你只需要从表格中做到这一点。

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