part 3 – adding and enabling a menu strip

 

in this post i will expand on my other posts (1 & 2 ) and show how we can add a menu strip and use it from vfp. you will need to download the source code supplied in the previous post to follow along.

 

mike s has requested this segment so here ya go mike…

 

open up the project in vbexpress.net and you are good to go.

 

adding a “menu strip” is very easy. double click the file mytoolstrip.vb to open it in the design window. from the toolbox locate the menustrip control and select it. click on your form to add it to the form.

 

for this example we will use the defaults provided same as the tool buttons. so right click on the menustrip and select “insert standard items”. a standard set of menu items will be added as shown.

 

 

in the code window (double click anywhere on the control or from the menu view|code), just below the last event you published, add this event:

 

'please enter any new code here, below the interop code

    ' these events will be published

    public event newdocument()

    public event open()

    public event save()

    public event print()

    public event cut()

    public event copy()

    public event paste()

    public event help()

    public event chkonoffchanged()

    ' handle all menu events here   <<<--- this is what you need to add

    public event menuitemselected(byval menuname as string)  

 

the final thing is to create events for each menuclick.

to do this just double click each menu item in design view, and this will open the code view with the click method setup. all that is needed is to paste the following line (bold) into each method:

 

private sub newtoolstripmenuitem_click(byval sender as system.object, byval e as   system.eventargs) handles newtoolstripmenuitem.click

        raiseevent menuitemselected(sender.name)

end sub

 

i have done this for only the file menu items. you will need to do this for all menu items.

 

once that is done just rebuild the control. we have finished with vb.net.

 

in vfp open your testform and see that the form control now contains the menu items as well.

 

 

all that is needed now is to wire in the menu handler. so in vfp, open the code window for the olecontrol.menuitemselected. this is the new event we exposed earlier. the code window opens with the parameter statement already there. this is because we specified that there was a parameter to be passed by the event when we exposed this event in vb.

 

add the following snippet of code.

 

*** activex control event ***

lparameters menuname

local cmenu as string

cmenu = upper(strtran(menuname,"toolstripmenuitem",""))

if cmenu = "exit"

      thisform.release

else

      wait window nowait "you selected menu item :"+cmenu

endif

 

basically you have to handle the menu clicks here in one place. this can easily be achieved using a case…endcase statement for each menu item. the name of the menu item is passed so i just strip off the excess to reveal the menu clicked.

 

now run your form and begin using the menu. notice that exit actually quits the form?

 

 

thanks to mike s for his suggestion. actually mike, this was on my “nextpost” list 🙂

One Response to Using the Interop Forms Toolkit in VFP9 III

  • MikeS says:

    Thanks Bernard.

    Man you were fast with this update! I’d actually got the menu strip into the .NET project and was able to see it on my VFP form. The part that I was missing was how to actually make a menu selection do something in VFP. Now that you’ve revealed how to actually rig up the code, it time to get busy!

    I haven’t tried anything yet, but, would I be able to dynamically enable/disable options (like VFP’s skip for condition)? How about things like cut/copy/paste that VFP’s menu handled for us? Do I need to now write code for them?

    Thanks again for blogging this stuff. What’s up your sleeve for the next installment?

    You ask, I deliver… I’ll look at Skip for and Copy..Paste. Lets see what I come up with.

Leave a Reply

Your email address will not be published. Required fields are marked *