简体   繁体   中英

c# object type conversion

I'm trying to create a context menu which will be able to add extra menu items, with an attached child menu, as required. I've been trying to do it so I have separate classes building up each part so it can be written nicely with objects.

The problem I am having is that the AddRange method for ContextMenuStrip doesn't have a constructor to deal with my object. I've tried converting it to ToolStripMenuItem type with operators which has not worked, as I suspected it wouldn't.

I am sure this can be achieved so I assume I've thought something through wrong. Is there a way to get around this or I banning my head against a wall with my current structuring?

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();
    }

}

}

You can't add anything else than a ToolStripMenuItem as child of a ToolStripMenuItem .

Don't spend time trying to inherit your ChildMenu and ParentItem classes from ToolStripMenuItem . Just add ToolStripMenuItems and drop these classes.

EDIT :

I would do something like the following (actually compiles):

public void PopulateMenu(string fileName, ContextMenuStrip parent)
    {
        ToolStripMenuItem newMenu = new ToolStripMenuItem(fileName);
        newMenu.DropDownItems.Add(new ToolStripMenuItem("View File"));
        newMenu.DropDownItems.Add(new ToolStripMenuItem("Delete File"));
        newMenu.DropDownItems.Add(new ToolStripMenuItem("Remove File"));

        parent.Items.Add(newMenu);
    }

Then for each of your file:

this.PopulateMenu(the_file_name, the_parent_menu);

Also don't forget to attach handlers to the Click event of your menus, or they'll be useless ;-)

In order to convert your object to a different type, like SystemObject newObject = (SystemObject)myObject those two types will need to have some relation to one another. Depending on the circumstance they can either have a common Interface or have some ancestry (if your type inherits from the other type, for example).

In your specific example your object would need to inherit from the ToolStripMenuItem object in order to be converted to one.

I'm sorry it's not better news, I hope that might help you a little though :)

On rereading my answer

I realise I might be teaching you to suck eggs and you're actually looking for a way to use AddRange and an object without a relation to ToolStripMenuItem, so please accept my apologies if that is the case :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM