How to create dynamically a menuStrip ?

Posted by rtardivo on 15-Oct-2009 03:15

Hi all,

I'm trying to create dynamically the main menu for my application and using the MS menuStrip.....  without success....

I'm searching for code examples

Do we have somewhere samples about this control ? or maybe could you share some code.

TIA

Richard

All Replies

Posted by Peter Judge on 15-Oct-2009 08:51

I'm trying to create dynamically the main menu for my application and using

the MS menuStrip..... without success....

I'm searching for code examples

Do we have somewhere samples about this control ? or maybe could you share

some code.

The easiest way (I found) is to create the menu with the designer and see what code is written out. You can then use that as a template for writing your dynamic loaded.

I did a DevCorner WebEx on inherited controls a few weeks - um, wow, make that a month - ago, and that dynamically created menus, but it was from the UltraControls (not Microsoft). The code for that is up on the Communities page at http://communities.progress.com/pcom/docs/DOC-102281.

-- peter

Posted by jmls on 15-Oct-2009 09:00

I did write some code a while (nearly 18 months!) ago to grab a standard progress menu bar and create an UltraToolBar menu. Maybe that would give you some sort of starter  / pointer ....

http://communities.progress.com/pcom/message/26053#26053

Julian

Posted by rtardivo on 19-Oct-2009 02:45

Here is my code

METHOD PRIVATE VOID loadMenu():
     DEFINE VARIABLE newMenuItem AS System.Windows.Forms.ToolStripMenuItem  NO-UNDO.
        DEFINE VARIABLE newSubItem  AS System.Windows.Forms.ToolStripMenuItem  NO-UNDO.
        DEFINE VARIABLE newSubItem2 AS System.Windows.Forms.ToolStripMenuItem  NO-UNDO.
     DEFINE VARIABLE Separator1  AS System.Windows.Forms.ToolStripSeparator NO-UNDO.
    
     DEFINE VARIABLE iTemp1 AS INTEGER NO-UNDO.
    
  DEFINE BUFFER ttmnu1 FOR ttmnu.
        DEFINE BUFFER ttmnu2 FOR ttmnu.
  iTemp1 = 10001.

    FOR EACH ttmnu WHERE ttmnu.pare="":
     newMenuItem = NEW System.Windows.Forms.ToolStripMenuItem(ttmnu.nomp).
     newMenuItem:tag = ttmnu.nomp + CHR(1) + STRING(ttmnu.nump).
        newMenuItem:Click:SUBSCRIBE(THIS-OBJECT:testClick).
     menuStrip1:Items:Add(newMenuItem).

        ASSIGN ttmnu.ident    = iTemp1
               iTemp1         = iTemp1 + 1.

        FOR EACH ttmnu1 WHERE ttmnu1.pare=ttmnu.fils:
            IF ttmnu1.nomp BEGINS "rule" THEN DO:
                toolStripSeparator1 = NEW System.Windows.Forms.ToolStripSeparator().
                newMenuItem:DropDownItems:Add(toolStripSeparator1).
                NEXT.
            END.
           
            newSubItem = NEW System.Windows.Forms.ToolStripMenuItem(ttmnu1.nomp).
      newSubItem:tag = ttmnu1.nomp + CHR(1) + STRING(ttmnu1.nump).
            newSubItem:Click:SUBSCRIBE(THIS-OBJECT:testClick).
      newMenuItem:DropDownItems:Add(newSubItem).

            ASSIGN ttmnu1.ident    = iTemp1
                   iTemp1          = iTemp1 + 1.
                  
            FOR EACH ttmnu2 WHERE ttmnu2.pare=ttmnu1.fils:
                IF ttmnu2.nomp BEGINS "rule" THEN DO:
                    toolStripSeparator1 = NEW System.Windows.Forms.ToolStripSeparator().
                    newSubItem:DropDownItems:Add(toolStripSeparator1).
                    NEXT.
                END.
               
                newSubItem2 = NEW System.Windows.Forms.ToolStripMenuItem(ttmnu2.nomp).
          newSubItem2:tag = ttmnu2.nomp + CHR(1) + STRING(ttmnu2.nump).
                newSubItem2:Click:SUBSCRIBE(THIS-OBJECT:testClick).
          newSubItem:DropDownItems:Add(newSubItem2).

                ASSIGN ttmnu2.ident    = iTemp1
                       iTemp1          = iTemp1 + 1.
            END.
        END.
    END.

  RETURN.

END METHOD.

  METHOD PRIVATE VOID testClick( INPUT sender AS System.Object, INPUT e AS System.EventArgs ):
      DEFINE VARIABLE monItem AS System.Windows.Forms.ToolStripMenuItem NO-UNDO.
      DEFINE VARIABLE monPrg  AS Progress.Windows.MDIChildForm NO-UNDO.
        DEFINE VARIABLE childForm AS Progress.Windows.Form NO-UNDO.
     
      DEFINE VARIABLE cTagItem AS CHARACTER NO-UNDO.
      DEFINE VARIABLE iNumMenu AS INTEGER   NO-UNDO.
      DEFINE VARIABLE cForm    AS CHARACTER NO-UNDO.

      ASSIGN monItem  = CAST(sender,System.Windows.Forms.ToolStripMenuItem)
             cTagItem = monItem:tag
             iNumMenu = INTEGER(ENTRY(2,cTagItem,CHR(1))).

      IF iNumMenu <> 0 THEN DO:
          FIND ttmnu WHERE ttmnu.nump = iNumMenu NO-ERROR.
          IF AVAILABLE ttmnu THEN DO:
/*              MESSAGE type-of(sender, System.Windows.Forms.ToolStripMenuItem) SKIP*/
/*                    monItem:Tag SKIP                                              */
/*                    INTEGER(ENTRY(2,cTagItem,CHR(1)))                             */
/*                    VIEW-AS ALERT-BOX.                                            */

                FIND ttprocedure WHERE ttprocedure.nump=ttmnu.nump NO-LOCK NO-ERROR.
                IF AVAILABLE ttProcedure THEN DO:
                    IF ttProcedure.procFile MATCHES "*.cls" THEN DO:
                        cForm = SUBSTRING(ttProcedure.procFile,1,length(ttProcedure.procFile) - 4).
                        childForm = DYNAMIC-NEW cForm ().
                        childForm:TEXT = ttmnu.nomp.
                        childForm:MdiParent = THIS-OBJECT.
                        childForm:FormClosed:SUBSCRIBE(Form_Closed).
                        childForm:Show( ).
                       
                    END.
                    ELSE DO:                   
                        RUN VALUE(ttProcedure.procFile) PERSISTENT SET ttProcedure.procHandle.
   
                        monPrg = NEW Progress.Windows.MDIChildForm(THIS-OBJECT,ttProcedure.procHandle:CURRENT-WINDOW).
                        monPrg:MdiParent = THIS-OBJECT.
                        monPrg:TEXT = ttmnu.nomp.
                        monPrg:Show().

                        DYNAMIC-FUNCTION('setMenu' IN ttProcedure.procHandle,this-object).
                        RUN initializeObject IN ttProcedure.procHandle.
                        ttProcedure.procID = ttProcedure.procHandle:UNIQUE-ID.
                    END.
                END.
                ELSE DO:
                    MESSAGE "Programme inexistant déf"
                    VIEW-AS ALERT-BOX.
                END.
          END.
        END.

     RETURN.
    END METHOD.

This thread is closed