Friday, October 14, 2005

A solution for converting C++ MFC code to C# or VB.Net

As Program Director for the Lansing MI based Great Lakes User Group for .Net (http://www.glugnet.org), I received an email from a member that has found a very useful method to accomplish a needed conversion from MFC C++ code into C# or VB.Net. While C++ MFC is outside my area of expertise, I read through his explanation and it sounds like a very useful technique. For those who can benefit, I have included the text of his email below. If you wish to explore this topic further, post a comment and I'll be sure that he is asked to contact you. Thanks. Joe Kunk



Greetings!

As a .NET user group member I wanted to share my experience of"migrating my MFC apps to .NET using C# and VB.NET" so that it will be beneficial for all our members. It might not be an ideal solution for all but I am hopeful it will add at least few new pointers in your knowledge base.

The problem was "How to migrate MFC applications to .NET in C# orVB.NET?" and there were no guidelines or samples. As we know MFC will besupported in Windows Vista (earlier codenamed longhorn) so we can keep MFC apps running for at least few years but question was how to enhanced and use with .NET managed assemblies, keep maintenance low, less compatibility issues, less interops, less performance hiccups and less complicated install program.

Above all new customers were asking on which technology our product wasbuilt? And if answer was "MFC" then we started loosing new business opportunity. Then what to do? What is the trend? Most of C++ programmershave already moved to C# and wish to convert their apps in C# or VB.NET but again there is lack of information on how to do it.

My first problem was ATL or MFC UI conversion to Windows Form resources and there was no help and that was the reason we never went ahead with even prototype test for migration. But thanks to Google where I found acompany called DudeLabs( http://www.dudelabs.com )which has a product"RC Converter" and it changed our migration policy because only in minutes we had our hundreds of dialogs, form views, all standard controls with their properties, menus, string resources and accelerators converted in Windows form resources and we chose our own class name,file name, namespace name and control names naming conventions. We used C# on Visual studio 2005 format but it also generates Visual studio 2003 format and supports C# and VB.NET both.

This quick conversion of our UI gave us ample time to focus on business layer and data layer rewrite.

Now thanks to Microsoft whose application block for SQL Data Access fixed our data layer rewrite in minutes because we were using MS SQLServer 2000. But even if you are using other databases please use SQLData Access application block code as sample and you will be done in hours or in worst case 2 or 3 days.

Business logic rewrite needs to be done manually and we knew that so we started rewriting it and let me tell you writing code in Visual Studio 2005 takes one third or less time as compared to MFC or ATL and then if you have any issues visit http://www.codeproject.com orhttp://www.GotDotNet.com and be assured you will find your solution there and if not the exact answer then at least you will find something to start with. I am really happy as we are near completion of our product migration from MFC to .NET managed world.
As .NET user group and our .NET community always helped me in my migration path I wanted to share it with you all so that it will make your migration at least a little bit easier.


What I learned was - It takes less time and money to migrate to .NET from MFC/ATL than we are estimating in present scenario.

But be careful as only migration to .NET will not increase you sales immediately because this migration first makes sure that your product will survive in future and then it increase your new sales opportunities and gives you flexibility to add new functional modules with less development time, ability to interact with managed world tools and components, enhances performance, keeps you on cutting edge technology, provides you click once deployment and has a very low maintenance cost.

So what you are waiting for?

Wish you good luck with your migration!

Monday, October 10, 2005

Finally a great utility for managing Internet bookmarks

I work on several computers, a laptop, a desktop, a server, etc. It has always frustrated me that my Internet Explorer bookmarks are different on each PC. For years I have wanted access to all my saved bookmarks, on any of my computers that I routinely use, or from any other computer via a web page.

I finally found it. The web site http://www.sync2it.com/ allows you to create a free member account where a master copy of all your bookmarks are stored. You download a small utility for your system tray that allows you to upload your current bookmarks and then provides easy one-click access to your bookmarks at any time. Add or delete a bookmark? The other computers are automatically synchronized with the same change. It is as if all the bookmarks are coming from a single shared directory. Wonderful!

If you are on a computer that does not allow you to install software, like a library PC, you can access all your bookmarks as a web page by signing into your account on the Sync2It web site.

And best of all, this service is provided free of charge.

This service has made my internet browsing so much more productive - I just love it. Try it out, I am confident you will be impressed.

Joe Kunk

Wednesday, October 05, 2005

Copy text contents of a listbox to the Windows Clipboard in VB.Net

When debugging a new program, I like to add log style messages to a listbox on a form as a very easy way for me to see what is happening with the program and verify if the proper actions are occurring. Recently I had a program that generated a LOT of messages and I wanted to copy the text contents of that listbox to the clipboard with a button so that I paste the text into WordPad and have a hardcopy record of the program's execution log. As with many things in VB.Net, it turned out to be very easy to do.

In case you have a similar need in the future, the code is listed below for your use. I hope you find it helpful.

Joe Kunk

'Copy contents of a string Listbox to the clipboard
Dim strList(Me.lbStatus.Items.Count) As String
Me.lbStatus.Items.CopyTo(strList, 0)
Dim strClip As String = String.Join(vbCrLf, strList)
Clipboard.SetDataObject(strClip)

A VB.Net function to return the text contained within the first set of quotes in a string

I needed a quick function to return the text within the first set of quotes in a particular string. In case you need a similar function at some point, it is listed below for your use. I hope you find it helpful.

Joe Kunk



'Returns the text within the first set of quotes found
Function GetTextWithinQuotes(ByVal strSource As String) As String
Const Quote As Char = Chr(34)
If strSource.IndexOf(Quote) = -1 Then Return String.Empty
Dim StartPos As Integer = strSource.IndexOf(Quote)
Dim EndPos As Integer = strSource.IndexOf(Quote, StartPos + 1)
If (EndPos - StartPos = 1) Then Return String.Empty
Return strSource.Substring(StartPos + 1, (EndPos - StartPos - 1))
End Function

A better error message from Try-Catch-Finally in VB.Net

I like to use Try-Catch-Finally blocks in my code in order to provide a more pleasant user experience. When I show an error message to the user, I like to include enough detail that if the user sends me a screen image, then I have a good headstart on solving the problem. I have played with several variations of the 'perfect' error message, and finally have settled on the format below that is user-friendly and still provides lots of needed detail information. I hope you find it helpful.

Dim ErrorMsg as string = "I was unable to perform <>." & vbCrLf & "Please make sure to <>." & vbCrLf & "Suggestions go here." & vbCrLf & vbCrLf & "Detailed Error Information below:" & vbCrLf & vbCrLf & " Message: " & exp.Message & vbCrLf & _" Source: " & exp.Source & vbCrLf & vbCrLf & " Stack Trace:" & vbCrLf & exp.StackTrace

Joe Kunk

Tuesday, October 04, 2005

Blank page when setting a tabcontrol's tabpage by its index

I am working on an application that has 30-40 tabs in a tab control. To keep it reasonable for the user, I need to limit the displayed tabs to just the 4-5 tabs that are relevant to the current action.

First I tried the obvious of just setting the visible property of the unwanted tabpages to false. This hid the contents of the tabpage, but the tabpage itself remained visible and in the row of tabs that could be selected by the user. If the tabpage had fully hidden itself as it should have (in my mind), I would have been all set. But instead I had a problem to solve.

To clarify, I have almost 40 tabpages total in the tabcontrol. There are four tabpages that always should be shown in a particular order (tabpages 2-5) and I wanted the first tabpage on the left to be different and selected depending on which item was selected in a tree control on the form.

In other words, I want all 40 tabs on the tabcontrol for ease of maintenance on the form, but to only show 5 of those tabpages at any time and to be able to dynamically change which tabpage from the non-display tabpages was to be shown at the first tabpage, depending on what is selected on a tree control on the form.

The first issue was that I could not insert a tabpage to a certain position. The .add method only adds the tabpage to the end of the existing tabpages collection.

The second issue was that any direct assignment to a tabpage always produced a blank tabpage, i.e., none of its child controls were visible and all attempts to get them to appear failed. That was disappointing as I could have just added a dummy tabpage as the first one and then repeatedly replaced with with the tabpage of current interest as the user moved around the tree control.

Utilimately I created a second non-visible tab control on the form named tcHidden, moved all the tabs over to it at Form_Load, then moved the tabpages between the two controls in the desired order as the user moved around in the tree control. It works well and it is fast enough that the users are happy with it.

The tabcontrol is one of those controls that seems to give me fits, probably because I always try to push it to its limits, so I wanted to post one of my workarounds in case someone else has a similar need. If you found this helpful, please post a comment and let me know. Thanks.

Note that I am using Visual Basic .Net 2003 and Framework v1.1