Custom menu items serialization ...

Hy!

I have build in VB .NET a custom menu which displays icons to each menu item. The StyleMainMenu inherits MainMenu and StyleMenuItem inherits MenuItem. For StyleMainMenu I have a toolboxbitmap and I can choose to insert directly from toolbar a style menu to the form. But, at design time, when I add menu items, they are made as "new MenuItem", not as "new StyleMenuItem".

Question: how can I make that, by design time, the menu items to be as StyleMenuItem

Thanks.

 



Answer this question

Custom menu items serialization ...

  • DiegoCrespo

     

    Hy!

    I have build in VB .NET a custom menu which displays icons to each menu item. The StyleMainMenu inherits MainMenu and StyleMenuItem inherits MenuItem. For StyleMainMenu I have a toolboxbitmap and I can choose to insert directly from toolbar a style menu to the form. But, at design time, when I add menu items, they are made as "new MenuItem", not as "new StyleMenuItem".

    Question: how can I make that, by design time, the menu items to be as StyleMenuItem

    Thanks.


  • MSDN Forum User

    Hello Rick ( posting to two forums !) I've got it working ( sort of ! ) with the following attributes added just above the property declaration for MenuItems

    [EditorBrowsable(EditorBrowsableState.Always)]
    [Browsable(true)]

    you have to close down VS for the changes to take effect though, before I added these attributes the property "MenuItems" was not visible in the prop sheet for the menu, now it is, and has a button (...) which adds the correct ( i.e. mine ) menuitem class BUT ! only for the menu pads, if you click on a pad and obey the "type here" prompt the standard items are still added, I'll keep at it though, my next step is to try your suggestion

  • Alberto Borbolla MVP

    Hi Pete,

    There's a property of CollectionEditor you may want to override -CollectionItemType

    In the constructor you may want to set this to typeof(PJKBaseMenuItem)

     



  • Gabe Covert

    Hy!

    I have tryied your code, but it doesn't function. First of all, I have my control in VB .NET. I have translated the code for VB, I also tryied in C#, but I can see no changes. Second, the "UIEditor" type doesn't exists, maybe it is "UITypeEditor". I have tryied also whit that, but no results.

    What is the problem in the code

     


  • ewillie

    If it's any consolation I can't get it to work either, have you got it working yet as this is exactly what I'm trying to achieve, regards

  • Jackie8640

    Hy!

    My code looks like the code from Peter Kane, but, as he also sad, it does't work. I have it tested in C# and also in my application in VB .NET. Why

    Thanks.


  • naiad

    PMFJI but !, Hello there , I to am trying to achieve this functionality, this is how I've interpreted your code suggestion

    // My base menu classes.
        public class PJKBaseMainMenu : MainMenu
        {
            [Editor(typeof(PJKMenuItemCollectionEditor),typeof(UITypeEditor))]
            [EditorBrowsable]
            public new Menu.MenuItemCollection MenuItems
            {
                get
                {
                    return base.MenuItems;
                }
            }
        }

        public class PJKMenuItemCollectionEditor : CollectionEditor
        {
            public PJKMenuItemCollectionEditor() : base(typeof(Menu.MenuItemCollection))
            {
            }

            protected override Type[] CreateNewItemTypes()
            {
                return new Type[] {typeof(PJKBaseMenuItem)};
            }
        }

        public class PJKBaseMenuItem : MenuItem
        {
        }

    what am I missing , if I add a new menuitem the generated code is new System.Windows.Forms.MenuItem , any help much appreciated

  • vikionline

    The base type is UITypeEditor, not UIEditor. That was a typo. If you have made that correction, it should be working. Make sure you close and re-open Visual Studio after adding this code and recompiling; Visual Studio does not always automatically recreate editors or converters internally.

    If that doesnt work, you will probably have to post your exact code.



  • PalMan

    Insert this into StyleMainMenu:

    [Editor(typeof(StyleMenuItemCollectionEditor), typeof(UIEditor))]
    public new Menu.MenuItemCollection MenuItems
    {
         get
         {
              return base.MenuItems;
         }
    }

    Then in a separate class:

    public class StyleMenuItemCollectionEditor : CollectionEditor
    {
         public StyleMenuItemCollectionEditor() : base(typeof(Menu.MenuItemCollection))
         {
         }

         protected override Type[] CreateNewItemTypes()
         {
              return new Type[] {typeof(StyleMenuItem)};
         }
    }

    Both of these require System.ComponentModel.Design. The new property is required because you need a property in StyleMainMenu that you can apply a custom editor to, and MainMenu.MenuItems is not overridable. But since you are just passing through the MenuItems collection from the base class, you are not changing any run-time functionality.


  • Custom menu items serialization ...