the final piece of the puzzle

one of the things i have always wanted to do was to be able to expose the objects contained in the usercontrol as well as their properties without writing thousands of set and get statements. after all if you take a simple control with 4 textboxes, there are literally thousands of properties that would require sets and gets to be exposed using the interop toolkit template. i always knew there had to be an easier way. well the other day while re-reading this article(http://www.sweetpotatosoftware.com/spsblog/commentview,guid,3dd24f92-a52c-4bb0-8121-c2e6e2cc4f93.aspx) from craig boyd and this line popped out at me:

 

“also, i've provided an optiongroup that will set the rendermode of the toolstrip and menustrip just to show that we aren't limited to events that we have exposed through the interface. public objects and their properties are accessible as well.”

 

so i downloaded craig’s c# sample and looked at the code.  two things i noticed – craig was defining all the objects as public and using the autodual attribute for the class. so this is what i tried to do and succeeded. this blog entry is about how this can be achieved. follow along using vb express.net or the full blown vstudio.

 

start up vb.net and create a new interop user control project. name the project myvbbuttoninterop and save it in a separate directory. i prefer my own directory since i hate the paths ms choose as default.

open the design file and add a button as below.

double-click the button to open the code window and add this simple code there:

compile your project. switch to vfp and open a new form. insert an activexcontrol object and look for your newly created activex in the list.

 

select it to add it to the form. open the properties window and see that there are very few properties exposed

run the form and click the button. nothing happens. this is because we have not wired in any events for exposure.

 

quit from vfp. note because the dll is cached in vfp if you need to change it (and we do) you need to completely close vfp. do this every time throughout this exercise.

 

back to vb and in the code view for the control if you scroll up and expand the vb6 regions you will find the area where you should add your code to wire in your methods. note that as of writing i have not found proper a way around this. you have to expose your events here so add this code in this section

 

and in the click code of the button change it to this

 

build again and when you open the form in vfp you will find that there is a new event available called buttonclicked.

 

double click the activex and in the buttonclick event add some of your own code

 

run the form and click the button. a message box appears

and when you click to close it your code fires and you see the wait window

 

close vfp and back to vb. we are now going to expose all the properties with just a few steps.

 

towards the top of the interopusercontrol.vb you will find this:

 

<comclass(myvbbutton.classid, myvbbutton.interfaceid, myvbbutton.eventsid)> _

public class myvbbutton

change it so that it looks like this:

build the project and try to open the form in vfp. you may get this error

no problem. the registration failed. so we use regasm with the /codebase option to register it. regasm is the utility used by net to register activex as we used regsvr32 for other activex. it can be found in your .net directory so open a command prompt and navigate to it.

run regasm on your dll which is found here

 

and this is the result you will see

the activex has been registered, but you see this warning message about being unsigned.

lets sign the assembly. for this you need to create a strongname pair that you can use to sign your assembly. you create it using the file sn.exe that is in the sdk which you can download from ms. we will do that later.

 

back in vfp open your form and it opens without any error. but check out the property sheet of the control. there are heaps more properties here since we changed the interface to autodual. but these properties are only for the container not the button which is what we want.

back in vb (you did close vfp completely?) and we need to enable the option to show all files. vb  hides them from us. so click the cotton in the project pane to show all files, expand the interopusercontrol.vb node and open the file that was hidden so long called interopusercontrol.designer.vb.

 

when you add any object to the control, in the background vb generates the code to implement this object. in our case it is the button added and is implemented here, always friend by default.

 

since the autodual interface will expose all public objects, we need to change this declaration to public if we want to see it in vfp.

 

compile the control and open the form in vfp again. you will not need to re register it with regasm.

 

now in vfp when you start typing, intellisense comes in and you find a new object called button1. this is our button we declared as public. you can now change all its properties from within vfp. you just don’t get intellisense.

 

so go ahead and change the code to this.

 

run the form and click the button. the message box appears, your wait window message next and finally the button is disabled!

 

voila we have exposed the button and all its pem’s to vfp with just a few changes.

 

let’s try this with a property of the container control. add a button to the form and add code like this to it

why backcolor_2 ? well i am not sure but i think this is because of overloaded methods used by .net.  run the form and click the new button

select a colour and see that you also have the properties of the container exposed as well.

 

but this is only a button and vfp already has buttons. ah, but what if you dropped a toolstrip control and then replaced all the friend declarations with public? then every object on the toolstrip would be exposed allowing you complete control without all those thousands of sets and gets. now that exercise i leave to you since that was the whole purpose of this post – to show how to expose pem’s with just a few lines of code.

 

one final piece is to get the assembly signed with a strong name.

 

first we need to create the file for signing and we use sn.exe from the .net ssdk for this. it is found here on my computer. download and install it.

so open a command prompt there and enter this command:

 

and then copy the generated file into your project directory

.

now we need to reference it in our project so back in vb (with vfp closed) open the file assemblyinfo.vb

 

and just below the other assembly attributes, add the line as shown. make sure the full correct path is entered to your .snk file

 

now rebuild and when you run regasm again you get no errors or warnings. our assembly has now been signed with a strong name.

 

here are a few links i found useful when authoring this post.

 

http://www.sweetpotatosoftware.com/spsblog/commentview,guid,3dd24f92-a52c-4bb0-8121-c2e6e2cc4f93.aspx

 

http://www.sweetpotatosoftware.com/spsblog/permalink,guid,df3f1be8-62bc-491c-935e-570ebfe53164.aspx

 

* about autodual

http://www.programmersheaven.com/2/vbrad-interop

 

* adding the strong key in vb

http://visualbasic.about.com/od/usingvbnet/a/fwtools5_2.htm

 

http://www.15seconds.com/issue/060309.htm

 

http://support.microsoft.com/kb/313891

 

some final notes:

with just a few lines i have shown how you can expose your .net activex objects without bothering with set's and get's. of course you will also find a huge number of other pem;s exposed, but just ignore them.

 

you will unfortunately have to manually expose all your methods as shown. there is an invoke method exposed but as yet i have not found out how to use it since it expects a parameter of type _delegate.

however now it is a simple matter of creating your controls and exposing key events. all the properties will be exposed if the techniques described her are followed.

i have attached the complete, signed dll along with the source, but it is better to start from scratch and diy as you will learn better. this code is for reference only.

unfortunately because of the amount of junk mail being generated from the weblog i have switched off comments. please post your comments, if any, at www.foxite.com

One Response to Using the InteropForms Toolkit in VFP9 – VI

  • Cesar says:

    Hi Bernard,

    Thanks a lot for this series of articles.
    It’s super well explained, the pictures show exactly the steps we need to follow, and the way you expose things, for sure will save us a lot of time. Thanks for sharing your experience! Being a blogger too, I know very well how much time and hard work it takes to create such an informative article.

    BTW, I’m just curious, did you already put some of this stuff in production?

    Apart from the toolstrip, have you tried other controls? Did you try any other more beautiful options for PageFrames or Grids?

    Regards

    Cesar

    Thanks for the comments Cesar. I started this because there was no free option for VFPers to easily get the toolstrip, and this would give some exposure for VFPers to .Net. No, I have not implemented these since working in a Terminal services enviro., memory is at a premium. But I have the knowledge if I ever need it and that is what I share here. And a picture is worth a thousand words anyway hence the profusion of pictures.Smile <img src=” src=”/emoticons/emotion-1.gif”>

Leave a Reply

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