Alternative to WPAMenuItem

[Top]  [Chapter]  [Previous]  [Next]


The WPAMenuItem class encapsulates some functionality which attaches a "wpa" action to a menu item. You can also use a few lines to replace this code, or to adapt the pricinple to work with other menu or toolbar controls.


The wpa commands provide an easy way to attach a menu to TextDynamic. The control does not only execute such commands, you can also retieve the caption and hint used for such an action.


Using WPDLLInt1.CurrMemo.wpaProcess( name, "" ) you can execute the command with the given name.


This makes it, using C# or VB.NET, very easy to create a menu on the fly - but you need to create a class which inherits from System.Windows.Forms.MenuItem and can store an additional value.


 internal class TagMenu : System.Windows.Forms.MenuItem


         public int Tag;



Now you can implement a function which adds a menu item as submenu of another menu item. (We created 4 menu items in the first level of the main menu which we plan fill)




This is the function which adds the items. It uses the introduced variable 'TagMenu.Tag' to store the id of the action.


 void AddToMenu(System.Windows.Forms.MenuItem menu, string wpa)


    if (wpa=="-") // Separator


                   TagMenu newmen = new TagMenu();

                   newmen.Text = "-";

                   menu.MenuItems.Add( 0, newmen );

    } else


            string caption = wpa;

            string hint = "";

            int wpa_id = WPDLLInt1.Memo.wpaGetID(wpa);




                                ref wpa, ref caption, ref hint);

                  TagMenu newmen = new TagMenu();

                  newmen.Text = caption;

                  newmen.Tag = wpa_id;

                  newmen.Click +=

                    new System.EventHandler(this.wpaMenuClick);

                  menu.MenuItems.Add( 0, newmen );                        





The function wpaMenuClick can be very short. This is all what is required:


 private void wpaMenuClick(object sender, System.EventArgs e)


         WPDLLInt1.CurrMemo.wpaExec((sender as TagMenu).Tag,"");



To fill our menu items we use this code - the items are added in reverse order.


         // Prepare File menu

                 AddToMenu(filemenu, "DiaPreview");

                 AddToMenu(filemenu, "PrintDialog");

                 AddToMenu(filemenu, "PrinterSetup");

                 AddToMenu(filemenu, "-");

                 AddToMenu(filemenu, "DiaPageProp");

                 AddToMenu(filemenu, "-");

                 AddToMenu(filemenu, "SaveAs");

                 AddToMenu(filemenu, "Save");

                 AddToMenu(filemenu, "Open");

         // Prepare Edit Menu

                 AddToMenu(editmenu, "Replace");

                 AddToMenu(editmenu, "Search");

                 AddToMenu(editmenu, "-");

                 AddToMenu(editmenu, "SelAll");

                 AddToMenu(editmenu, "Paste");

                 AddToMenu(editmenu, "Copy");

                 AddToMenu(editmenu, "Cut");

         // Prepare Insert Menu

                 AddToMenu(insertmenu, "DiaINSSymbol");

                 AddToMenu(insertmenu, "DiaINSGRAPHIC");

                 AddToMenu(insertmenu, "DiaINSTable");

                 AddToMenu(insertmenu, "DiaINSHyperlink");

         // Prepare Format Menu

                 AddToMenu(formatmenu, "DiaParagraphBorder");

                 AddToMenu(formatmenu, "DiaBulletOutlines");

                 AddToMenu(formatmenu, "DiaParagraphProp");



Similar code to the one above can be used to fill a toolbar as well. Please see the demo "MDI application" which shows how to use the ToolStrip control in VB.NET.

[attachmenu.htm]    Copyright © 2007 by WPCubed GmbH