我正在尝试创建一个上下文菜单,该菜单可以根据需要添加额外的菜单项以及附加的子菜单。我一直在努力做到这一点,所以我有单独的类建立每个部分,所以它可以很好地写入对象。c#对象类型转换
我遇到的问题是ContextMenuStrip的AddRange方法没有处理我的对象的构造函数。我试图将其转换为ToolStripMenuItem类型,但没有正常运行,因为我怀疑它不会。
我确信这可以实现,所以我认为我已经想到了一些错误的东西。有没有办法解决这个问题,或者我用目前的结构禁止我的头靠在墙上?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Context
{
class TestMenu
{
public TestMenu()
{
ContextMenuStrip filesToUploadContext = new System.Windows.Forms.ContextMenuStrip();
// Hot Folder Header
ToolStripMenuItem hotHead = new System.Windows.Forms.ToolStripMenuItem();
// Holder for files in Hot Folder
ParentItem hotFile; // foreach
// Dropped Files Header
ToolStripMenuItem dropHead = new System.Windows.Forms.ToolStripMenuItem();
// Holder for files that have been dragged and dropped in
ParentItem dropFile; // foreach
ToolStripSeparator toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
ToolStripSeparator toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
filesToUploadContext.Items.AddRange(new ToolStripItem[] {
hotHead,
toolStripSeparator1,
hotFile, // Not a toolStrip item
dropHead,
toolStripSeparator2,
dropFile // also not a toolStrip item
});
//// Testing stuff vv
//// Hot Folder
//hotFile.DropDownItems.AddRange(new ToolStripItem[]
// {
// viewHot,
// deleteHotFile
// });
//// Dropped Items Folder
//dropFile.DropDownItems.AddRange(new ToolStripItem[]
// {
// viewDrop,
// removeDropFile
// });
//// Hot Folder Section Heading
//hotHead.Name = "hotHead";
//hotHead.Text = "Hot Folder Files";
//hotHead.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
//// Drop Folder Section Heading
//dropHead.Name = "dropHead";
//dropHead.Text = "Dropped Files";
//dropHead.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
}
class ParentItem
{
// MenuItem to be used for found files
// Options will contain child items
public ToolStripMenuItem name = new ToolStripMenuItem();
public ChildMenu options { get; set; }
public ParentItem();
}
class ChildMenu
{
// Options available for specific files at end of menu tree
public ToolStripMenuItem view = new ToolStripMenuItem("View File");
public ToolStripMenuItem delete = new ToolStripMenuItem("Delete File");
public ToolStripMenuItem remove = new ToolStripMenuItem("Remove File");
public ChildMenu();
}
}
}
非常多的代码,但哪里不行? – 2012-01-05 09:21:04