00:01Hello, and welcome to Migrating from Visual Basic 6 to Visual Basic 2008.

00:06This is our second video and this is our case study #2, where we're going to convert...

00:11...and intermediate level ArcMap command.

00:15The functionality is going to be a button that has an image on it this time...

00:19...and it's going to launch a Windows form.

00:21And on that form, you will have the ability to wire up some events and to display a count...

00:26...of the number of selected records.

00:28I'm Don Kemlage, I'm a product specialist at Esri, so let's go ahead and get started.

00:34Well, this process is going to involve five steps.

00:38Our first step is going to be to test and review the original Visual Basic 6 project.

00:43Then we're going to use Visual Studio 2008 to convert the Visual Basic 6 project...

00:48...into a Visual Studio 2008 version.

00:53Then we're going to fix the code inside the Visual Studio 2008 project...

00:57...and we're going to test it for functional equivalence to make sure that it behaves...

01:01...exactly like it was supposed to inside of Visual Basic 6.

01:05Then we'll go through and we'll do some updates in the Visual Studio 2008 project...

01:09...and we'll try to get it to conform to some .NET standards and...

01:13...take advantage of some best practices.

01:15And then the final thing that we're going to do is we're going to port some of our code... a new Visual Basic 2008 project that takes advantage of the Esri base classes.

01:26And we'll see if we can even make development even easier by using these.

01:31So let's go ahead and get started.

01:34So on this part, we'll test and review the VB 6 project.

01:38Here we have a copy of ArcMap open with a default set of layers.

01:42We'll go head and open our Customize dialog and we'll choose Add from file.

01:50And we'll select our Project1.dll.

01:54This is the class we want to add.

01:56And in the Developer Samples, we'll see that we have an event VB 6.

01:59I'll drag it onto the GUI.

02:05And when I click, it launches a dialog where we can wire up...

02:09...the maps selection changed event.

02:12Before we click the Wire Up the Map's Selection Changed Event...

02:15...we need to make sure and set our first layer as being the only selectable layer.

02:20And the way to do that is we'll make our ArcMap application active.

02:24We'll right-click on the first layer and choose Selection...

02:28...Make This The Only Selectable Layer.

02:31And then we'll bring our Windows form back up.

02:34So we'll click this button -- it will ask us to go through and select some features.

02:41I'll select a few features.

02:46And the dialog says we have five counted.

02:48And you can select some more features.

02:52And the Selection Count changes.

02:56If we go over and look inside the VB 6 project that it took to make this functionality...

03:01...we'll find that we have a class, clsEvents of which it implements ICommand...

03:07...and this is standard polymorphic behavior.

03:10And we can go through and see all the different properties and methods...

03:14...that are available.

03:15One that I want to draw your attention to is the ICommand_Bitmap.

03:20And if we review the code, we find out that it's coming from frmResources...

03:25...the Image List1, the List Images, and then grabbing the first one...

03:29...and then it's getting the picture, and then the Handle -- and that's being returned back.

03:34And so if we were to go look at this, frmResources, we'll find that it's a...

03:39...Windows form and this is where the image list is.

03:42And if we open up its Properties, and then we go to the Images tab, we find that our image... added here, and this corresponds to the image that you saw on the ArcMap GUI.

03:54It should be noted that this form never actually displays.

03:57It gets created as part of the VB 6 project, but yet, a user would never interact with this.

04:03The only form that we interact with is the frmEvents...

04:07...and this has the functionality that we're used to.

04:11And if we look at the code behind...

04:14...we'll find that our hook gets passed in through this Property Let...

04:19...with the Application and then it goes through...

04:22...and we have a global variable of a Private WithEvents, which is...

04:25...m_pActiveViewEvents As Map.

04:31And it goes through and it gets the selection changed and displays the string...

04:35...inside of the Windows form and the Command1 Click just calls this event.

04:42At this point, we'll use Visual Studio 2008 to convert the VB6 project.

04:48So I have a Visual Basic 2008 project, and I will choose File > Convert.

04:53And I'll choose the Visual Basic 2008 Upgrade Wizard -- and I'll choose Next.

04:59I'll specify the project, which points to the VB 6 project -- and I'll choose Open.

05:07I'm happy with that -- I'll choose Next.

05:10It's going to be a DLL -- we'll choose Next.

05:13I'll specify an output project -- I'll choose Next.

05:24I'll want to create the folder now -- I'll choose Next.

05:31And we'll let the process run.

05:35And when the code converter's done, we'll have our project done of which we can save it.

05:41And we can see it on the hard drive.

05:49Now that our project has been converted to Visual Basic .NET...

05:53...we'll go through and do a compile and check for any errors...

05:56...and then test its Visual Basic equivalency.

06:00I like to start my making a copy of the original...

06:03...conversion projects, so I'll do a CTRL+C, and a CTRL+V, and I'll rename this... be VB2008_Equivalency.

06:14So we'll go ahead and open that up and open this version of the Solution File.

06:21The first thing we should do once the project is open, is build it.

06:26And we'll discover that we have five errors and one warning... let's go ahead and start making our changes.

06:39We'll start with clsEvents.vb.

06:45And the first thing we'll do is we'll rename the name of it to...

06:50... 2008 so we won't have a conflict with our VB6 project.

07:01All right, let's go see what our errors are.

07:03So the "Class clsEvents must implement ReadOnly Property Bitmap As Integer."

07:08We've seen this before in the prior case study, so we'll go through and we'll...

07:11...go immediately down to the ICommand bitmap and we're not going to...

07:15...pass back an OLE HANDLE; but rather, a system integer.

07:25It turns out that Windows forms behave differently... Visual Basic 2008 than they do in VB 6.

07:32And we're not going to be able to take advantage of this frmResources as it...

07:37...currently sits in order to obtain the handle of the image to be displayed on the button.

07:44So what we need to do is we're going to create some member variables...

07:49...and we'll do that near the top of the class file.

08:06And what these member variables are is we're going to have a m_frmResources... new frmResources.

08:15And if we go and we look at our Solution Explorer, we'll find that we do have...

08:19...frmResources, which was translated from before.

08:22We're also going to take advantage of a structure, which is a platform-specific type...

08:26...used to represent a pointer or a handle, so we're going to call this m_hBitmap.

08:32And we're also going to add an m_application to represent the IApplication variable...

08:37...that's going to be passed in as the hook.

08:41So let's go ahead and do some work to that ICommand_Bitmap property.

08:45What is coming out was originally provided so we can learn from it.

08:49And we're going to create an image list from the form, so we'll Dim imagelist As...

09:00...ImageList, and that's going to be equal to our m_frmResources.ImageList1.

09:09And we're also going to give image collection...

09:16...and that's going to be as an ImageList.ImageCollection.

09:22And that's going to come from our imagelist.

09:26And we use IntelliSense to get to our images, which returns an image collection.

09:34And we're going to go through and get the individual image.

09:42And that's going to come from our image collection.

09:45And we're going to get the first item.

09:48Notice how item (0) is the first item in VB.NET.

09:53And we're going to create a bitmap.

09:59And that's going to equal to our image cast as a bitmap.

10:10We need to set what the transparency is going to be on our bitmap as well.

10:14So we'll do that here.

10:20And we want the upper-left pixel to be the transparency point.

10:28And then, finally, we want to get our handle.

10:34And that's going to be equal to our bitmap.

10:44And we're going to want to return that.

10:50But we need to make sure and return it as an integer.

11:01Unfortunately, with Visual Basic 2008, because we're actually taking advantage...

11:06...of a pointer, we need to release the memory.

11:09And in order to do that, we're going to have to make a Windows API call.

11:13And I happen to have some code that I've had from before... I'll go ahead and copy that into the project.

11:18You can certainly learn more about that if you want.

11:21But basically what we're going to do is we're going to call a Private Shared Function...

11:25...called "DeleteObject."

11:26And we're going to pass in our IntPtr.

11:30And what it's going to be defined as is a system.Runtime.InteropServices.DllImport.

11:37And we're going to take advantage of the gdi32.dll.

11:41And this is basically establishing...

11:43...the signature for the Windows API call that we're going to make.

11:48And then finally, in order to release this memory, what we need to do... we're going to go through and we're going to take advantage of our Finalize.

11:57And inside of the Finalize, in fact I'm going to copy and paste to a better location.

12:07We'll put it right below the API call.

12:12We're going to change some of the behavior of the Finalize.

12:15And what we're going to do is we're going to say, If our (m_hBitmap.ToInt32() <> 0, then...

12:33...we're going to call the DeleteObject function.

12:37And we're going to pass in our (m_hBitmap).

12:45And that will do any garbage collection and free up any memory on the image...

12:49...once the ArcMap application is closed down.

12:52So at this point, let's see how we're doing on our errors.

12:55So we have two errors so far.

12:57We have the Property ICommand does not return a value on all code paths.

13:02Well that's probably east to fix, so why don't we go ahead and check that one...

13:04...and we'll just return -- and Nothing.

13:09Let's see what other errors we got: Reference to a non-shared member.

13:15So if we go back and we look at our ICommand_OnCreate... was passing in the hook to the frmEvents an application property.

13:26So if we were to go look at our Solution Explorer and go to our frmEvents...

13:32...and then look at the code for it, we see that we have an application property...

13:35...and we were passing it to it automatically.

13:38So we're going to have to do a little bit of changing there in order to keep this...

13:44...functionality consistent, so why don't we go ahead and we'll comment out that line for now.

13:49And why don't we go through and do an m_application...

13:54...member variable that we had set before, and why don't we set that equal to...

14:01...our hook variable that came through.

14:04Of course, we need to cast that to a specific type so that would be...


14:13And we'll just leave that as it is, and we'll actually put more of the logic of setting...

14:17...the Application inside of the OnClick event.

14:20So we'll change our frm.Events.Show...

14:27...and we'll change that to, we actually need to declare... instance of our frmEvents.

14:32We'll do frm As New frmEvents.

14:38And then we'll do frmEvents. and we'll be able to take advantage of our application...

14:47And we'll set that = m_ application.

14:52And then, finally, we need to show our frmEvents.Show.

14:57And so we'll just do our build one more time just to make sure that...

15:01...we did, in fact, get all of our errors.

15:04And if we check, we have no errors and no warnings.

15:06So at this point in time, why don't we go through and make sure and change...

15:10...some of the project properties: Events and our Root namespace as well.

15:20We need to check for COM interop in our Compile tab.

15:26And in our Debug tab, we want to set our external program to be ArcMap.exe.

15:38The last thing we should probably do is make sure there is no extraneous reference...

15:41...added to this project.

15:49And we see that there is quite a few, so let's go back and look and see what we had...

15:52...inside of a Visual Basic 6 project.

15:57We see that we only needed ArcMap, ArcMapUI, Carto, Framework, System...

16:02...SystemUI, and GeoDatabase.

16:08So back inside of our .NET project, let's get rid of the ones we really don't need.

16:14So we'll get rid of CartoUI, Catalog, DataSources, DataSourcesRaster, Display...

16:21...GeoDatabaseExtensions, GeoDatabaseUI, Geometry, GISClient, Output, and Server.

16:35Let's make sure our project can still compile.

16:40Everything looks good, so why don't we test it inside of ArcMap?

16:47And when ArcMap opens, we'll choose a default project.

16:54We'll add from file.

17:03We'll make sure and choose the .tlb file.

17:07Our objects are added with the Developer Samples category being selected...

17:12...we can copy our events VB 2008 onto our GUI.

17:19And we can test the functionality.

17:23And let's not forget to set our selectable layer.

17:25I'll do it an alternative way by doing Selection > Set Selectable Layers.

17:34So while we're at Events, I'll select some features.

17:41We have three selected -- we'll change to some additional ones.

17:46We'll hold down the SHIFT key and select even more -- and we find that we have 8!

17:53So our functionality is equivalent.

17:58At this point in time, we probably ought to update our project...

18:02...take advantage of some of the best practices that are available inside of .NET.

18:06Now, I did this before in the first case-study video...

18:09...and we spent a considerable amount of time doing that.

18:11I'm going to do the same types of changes in this project as I did in the prior one...

18:16...except I'm not going to go with the same pace; I'm going to put the video recording...

18:19...mode in a lot faster when I'm actually making the changes and I will talk less...

18:24...but I'll tell you ahead of time what I'm going to do.

18:26And that way, we can spend more time on the next section...

18:29...which will be about taking advantage of the Esri base classes.

18:33So let's go ahead and get started.

18:34I'll start by making a copy of the original Equivalency project...

18:38...and then we'll use that for the next set of editing.

18:50Now that the project's open, we'll go ahead and get rid of any extraneous files...

18:54...that aren't necessary for the running of the project.

19:02And now we'll get rid of any extraneous references...

19:04...that might be in the project that aren't necessary.

19:11So let's go ahead and rebuild the project to make sure there aren't any errors or warnings.

19:18And as we can see, we have a namespace compatibility.

19:21We actually saw this issue before...

19:23...and that's under the Project Properties and we'll go ahead and remove that.

19:34And let's rebuild our project.

19:38And all of our Errors and Warnings are gone.

19:40Let's open up our clsEvents.vb file.

19:43And we'll turn the Option Strict on and fix any casting errors if there are any.

19:50I will also, at this point in time, change the return arguments for each one of the... types instead of by its name by using the return statement.

20:22I'll change the name of the command...

20:24...and the caption so we don't have a conflict with our prior version.

20:32And I'll also put some comments in the code so it makes it more useful for the future.

21:14And at this point in time...

21:15...we'll build the project to make sure we don't have any errors or warnings.

21:21And everything looks good.

21:24There's one other class file that we're going to need to edit and make changes to...

21:27...and that's the frmEvents.vb.

21:34So let's go ahead and follow the same pattern of editing this code.

21:38First, we'll turn on the Options Strict and fix any casting errors.

22:04And we'll do a rebuild to make sure we got them all correctly.

22:10And we have no Errors or Warnings.

22:13And we'll go ahead and add the import statements near the top of the class file... that we can shorten our syntax as we're doing our editing.

22:22I'll also do some renaming of variables that I believe conform more to .NET standards.

22:49I'll also go through and do some code rearrangement such that the declaration...

22:52...and instantiation of objects can happen on the same line wherever possible...

22:56...and make the flow a little nicer.

23:32I'll also go through and shorten up some of the syntax... that it's not quite so long on the screen and requires less scrolling around.

24:14And I'll rebuild the project to make sure there's no errors -- no Errors, no Warnings.

24:22I'll also go through and do a little more code rearrangement to make some of the...

24:26...arguments look less complicated.

25:33And we'll rebuild the project to make sure there's no errors -- no Errors, no Warnings.

25:42I'll also add a few comments to the code to make it look more understandable...

25:46...for future longevity.

26:15And we'll rebuild the project.

26:19And we have no Errors and no Warnings.

26:22At this point in time, we can test the project inside of ArcMap... make sure that we still have functional equivalence.

26:40And here's our version 2.

26:45We'll set our selectable layers...

27:00...and we selected our features, so we have a functional equivalent.

27:05The final step that we want to accomplish is...

27:07...we want to take advantage of the Esri base classes.

27:09And we're going to port some code from the prior project...

27:13...and this is all going to be done inside of Visual Studio 2008.

27:16And this new project that we're going to take advantage of...

27:19...we're actually going to do some events a little differently, and we're also going to...

27:22...take advantage of the bitmap a little differently as well.

27:25So let's go ahead and get started.

27:28So with Visual Studio 2008 open, we'll choose File > New Project.

27:34We want a Visual Basic, ArcGIS, Desktop.

27:37We want a Class Library (ArcMap).

27:39We'll change the name to Events2008.

27:43And when the ArcGIS Project Wizard opens, this time we'll go ahead and add some...

27:48...references to our project, Alphabetical, and we'll select our references.

27:55We'll add ArcMap, I'll hold down the CTRL key, ArcMapUI, Carto, Framework...

28:09...GeoDatabase, System, and SystemUI, and we'll click the Add button and choose Finish.

28:18And once the project is completed, we'll delete the initial class file.

28:25And we'll choose Add > New Item, and we'll choose ArcGIS...

28:32...and we'll choose a Base Command and we'll give it the name of "clsEvents.vb"

28:39And we'll click Add; we'll choose Desktop ArcMap Command, and click OK.

28:47And at this point in time, we'll save our project.

28:50And we'll give it this name and in this location.

28:54You're going to notice that there's a bitmap that's created for us automatically...

28:57...and you can double-click to open it up and it looks like a bunny.

29:01We'll close that down for now.

29:03And, remember in the VB 6 project, we actually had our own image...

29:07...and we translated that into the into the vb.NET project.

29:12The problem with that was is we had to go through that and use some Windows API...

29:15...calls to delete memory when we were done with it, and we had to do some interesting...

29:19...tricks to take advantage of that image.

29:22So what we're going to do this time is we're going to take advantage of the image... this project, but in a different way.

29:28And I'm going to start off by deleting this existing bitmap; but, we'll remember its name.

29:37And we'll open up Windows Explorer and we'll go to the VB6 project...

29:41...and we'll copy the events.bmp file, which was in the VB6 project and we'll put it in...

29:46...inside of our new Events2008 project, so we'll do a CTRL+C...

29:54...put it right at this level, but we'll rename it.

30:01And the name that we will give it will be "cls.Events.bmp."

30:09We'll go back to our Visual Studio project.

30:14We'll expand out to everything and we'll notice that here's our clsEvents.

30:17Now, what we need to do is we need to include this in the project...

30:20...but that's insufficient; now, we need to right-click and choose Properties...

30:26...and make sure that the Build Action is an Embedded Resource.

30:31Now, if we go back to our Solution Explorer, and view the bitmap...

30:36...we'll see that we have the image that we're looking for...

30:39...and we'll rebuild a solution to make sure that there's no errors.

30:42And everything succeeds just the way that it should.

30:47At this point in time, we'll make some changes to the Base Class properties.

30:53And fill in our options for our category, our caption, and our name.

31:03If you remember back to the prior project, inside of the OnClick event...

31:08...we went through and we launched the Windows form, which did all the...

31:12...event procedure and had all the fun stuff that we were looking for...

31:15...inside of our ArcMap commands.

31:17So why don't we go back to our old project and why don't we copy the code...

31:22...from the OnClick event.

31:25And we'll copy that into our new project.

31:32And the first thing you're going to notice is there's no frmEvents.

31:36Well, why don't we borrow the code from the other project as well as far as its frmEvents... we don't have to do as much work.

31:43The way we can do that is we'll open up Windows Explorer.

31:48We'll go back to the old project, which was VB2008_Equivalency2.

31:53And let's copy our frmEvents dialog; we'll do CTRL+C and a CTRL+V...

32:05...and we'll go back into our new project, and we'll add this form.

32:09So we'll choose Add, this time we'll choose an Existing Item...

32:13...and we'll choose our frmEvents.

32:18Now, I want you to notice off the bat is notice how this icon looks like a little code file.

32:22It doesn't look like a Windows form -- well, why is that?

32:25We'll, if we expand our references, we will find that we don't have a...

32:29...System.Windows.forms, so we might as well go ahead and add that now.

32:35And now that the add reference dialog is open, we'll scroll down to the...

32:38...System.Windows.Forms and we'll add it to our project.

32:45Notice how our icon is now changed to a little form.

32:48And, also, our error message went away as well.

32:52So let's go ahead and open up our Windows form.

33:01And we'll see that everything looks just as it did in the prior project.

33:04Except this time, with vb.NET, we can actually take advantage of not only...

33:09...wiring up the map's SelectionChanged Event, we can also unwire it... remove the functionality as well while the project is actually...

33:18...running inside of ArcMap -- so let's show you how to do that.

33:22We'll increase the size of our form, and we'll make a copy of this button.

33:26I'll do a CTRL+C, and a CTRL+V; we'll move it down a little bit inside of the dialog.

33:33We'll do a right-click and choose Properties.

33:37We'll change the Text on it; instead of "Wire," we'll say "Un-wire."

33:42We'll also give it a name.

33:45We'll call it "BtnUnwire."

33:52And then we'll double-click on this button.

33:55I want to show you something different about wiring up events inside of...

34:00...Visual Basic.NET that can be done differently than Visual Basic 6.

34:04Now, while we could have used the Private WithEvents keyword to wire up an event...

34:10...that has some limitations in that you can't dynamically unwire events and there are...

34:14...some other limitations that you can read about in the documentation as well.

34:17I want to show you an alternative about how we can do that.

34:20So let's comment out that line right now and let's do it slightly differently.

34:24And the way that we'll do it is we'll say "private m_map_Events As"...


34:40But what you need to do, and you're going to have to read this inside the documentation... want to put another _Event on the end of it.

34:50And this will wire up our m_map_Events As an ESRI.ArcGIS.Carto...


35:01If you remember back in the prior project as well...

35:05...that there was a Private Sub m_pActiveViewEvents_SelectionChanged...

35:11...which did the Handles of the m_map_Events.SelectionChanged.

35:17What we're going to do is we're going to change the signature to the Sub...

35:20...but we'll keep all the functionality exactly the same.

35:24And that signature is going to be Private Sub OnActiveViewEventsSelectionChanged.

35:31But the functionality will remain the same.

35:33And what we'll do is we'll wire up a delegate that calls this function interactively.

35:39Let's see how we can do that.

35:41And this would be done inside of the Command1.Click... let's scroll down and find that.

35:46And the way the wiring was done before was m_map_Events was equal to the map...

35:51...and then we cast it to the map interface.

35:54This is going to be done differently.

35:55What we're going to do is we're not going to cast it to the map interface...

35:59...but we're going to cast it to the ESRI.ArcGIS.Carto.IActiveViewEvents_Event.

36:13And we're not done yet -- that just gets this variable hooked up.

36:16But we actually want to add the Handler, which does the delegate work.

36:20And that's done by doing the following...

36:23....AddHandler m_map_Events. SelectionChanged,AddressOf...

36:36...the name of the Sub we had changed the signature to above.

36:42So we can even just copy and paste that right here.

36:51And that's it!

37:35...our OnActiveViewEventsSelectionChanged and that's all we need to do.

37:41So let's go ahead and add a few code comments in here...

37:44...for longevity purposes and then we'll test the project.

37:49So here are some comments that I promised...

37:53...for about the EventHandler and how it's different, so you can read about that later...

37:58...through the EDN document series.

38:01And here are some comments for the AddHandler...

38:04...and some comments for the RemoveHandler.

38:06So at this point in time, we should be able to build the project and we have... Errors and Warnings, and we should be able to test this inside of ArcMap.

38:17And we'll choose Tools > Customize, and in our Developer Samples... is our Events VB2008 via Base Class; we'll copy that under the GUI.

38:31And we'll test the functionality -- don't forget to set the selectable layers.

38:39We'll click on the Wire Up the Map's SelectionChanged Event.

38:44Click Select Features.

38:49Notice four features are selected.

38:51I'll change it and select just two features.

38:55And notice only two features are selected.

39:00And now I'll click the Un-Wire the Map's SelectionChangedEvent.

39:05And I'll select some features.

39:09And notice our selection count didn't change, but yet if I go and wire up...

39:14...the Map's SelectionChanged Event and select some events...

39:18...we have a Selection Count changed.

39:20And this concludes our second series in our case studies.

39:24And I hope it's been instructive for you -- thank you.

Copyright 2016 Esri
Auto Scroll (on)Enable or disable the automatic scrolling of the transcript text when the video is playing. You can save this option if you login

How To Migrate a VB6 project to .NET - Complex Project

In this video, Don Kemlage from Esri's ArcGIS product team walks through the process of converting a project from VB6 to VB.NET. This focuses on migrating a moderately complicated ArcMap Command from VB6 to VB.NET. Topics of handling the Command's image, interacting with a user-created Windows Form, and wiring up events are discussed as one migrates from VB6 to VB.NET.

  • Recorded: May 13th, 2010
  • Runtime: 39:27
  • Views: 689
  • Published: Aug 18th, 2011
  • Night Mode (Off)Automatically dim the web site while the video is playing. A few seconds after you start watching the video and stop moving your mouse, your screen will dim. You can auto save this option if you login.
  • HTML5 Video (Off) Play videos using HTML5 Video instead of flash. A modern web browser is required to view videos using HTML5.
Download VideoDownload this video to your computer.
<Embed>Customize the colors and use the HTML code to include this video on your own website
Start From:
Player Color:

Right-click on these links to download and save this video.


Be the first to post a comment
To post a comment, you'll need to login.
If you don't have an Esri Global Login ID, please register here.