(Print this page)

A .Net Multi-Lines combo box (and how to convert code from C# to VB)
Published date: Sunday, June 23, 2013
On: Moer and Éric Moreau's web site

A good question was asked in my favorite forum (Experts Exchange not to name it).

The asker just wanted that each item offered by a combo box could display more than a single line. When I first saw the question, I thought is sounded easy but that feature is not natively supported by the control.

The original answer

I have used my favorite search engine to find out that not too many free options are available out there. I found a C# solution on another great forum that I have submitted to the asker but he really wanted a VB.Net solution. Being a VB MVP, I couldn’t tell to just use the C# version.

The downloadable demo code

The downloadable code you find here is both C# and VB versions of the multi-lines combo box. The solution has been created with Visual Studio 2012.

What were the difficulties in translating the code?

Instead of going through all the code, I will just tell you what I have done to convert the code and what the difficulties were.

When I only have a small snippet of code to translate (like in this case), I often use a code converter. None are perfect but my preferred converter is the free one from Telerik. You don’t need to install anything, just open the web site, paste your code, select the correct translator and click a button.

The converter did a decent job for the ComboBoxEx class (copy the whole C# class and you are good to go). Only 2 errors about incorrect type cast that were once removed were not preventing the compiler from doing its job. But this is not a guarantee that your job is fully done!

Now that the class is converted, you can try to convert the form. I copied the whole form’s code to the converter and brought it back to my VB.Net project and … found 11 errors. I told you that the converter is not perfect!

Most errors are easy to fix. Some modifications required to empty the Error List window. The most important one is that all event handlers are improperly converted.

After you emptied your Error List window, you hit F5 to run the application but … it is not running. Here are a few more things that you need to do.

If you copied the full code from the C# project, you probably have a namespace that might not be corresponding to what the project properties are expecting for the startup object. So open the project properties and set at least the startup object to something good.

Figure 1: Setting the StartUp form

Once you have passed this step, if you hit F5, you should be able to see your combo on the form. But this is not all! If you open the combo, your list will probably appear somewhere at the top left of your screen, a place that might not even be related to your form.

So there are some more problems to fix.

The first thing you should look at, and we have a couple in this project, is any DllImport declarations. Very often, the converter will be able to give code that compiles but that might not respect the “official” declaration. For any DllImport of any of the Windows librairies, you should visit another free source: PInvoke (hosted by Red Gate) This web site gives you many (if not all) calls to Windows libraries in C#, VB.Net and even VB6 along with many samples. Since it is a wiki, you can also contribute if you find something interesting.

In this case, the conversion seems straight-forward enough but it wasn’t. If you look quickly, only the return type was a bit strange. If you look again for the differences between the converted code and what is shown on PInvoke, you will find that a couple of keywords are missing in the converted declarations, namely ByVal and ByRef. The C# code has the “out” keyword which the converted didn’t properly convert to ByRef.

Replace the declaration with what you found on PInvoke and hit F5 again. It should be running as expected now.

Figure 2: The demo application in action

Conclusion

VB.Net and C# are almost on par when comparing there feature set.

When you find C# code and you need VB code, you have free tools helping you do the conversion. The tools are not doing a perfect job but it can save you a lot of typing.

I hope this article, on top of adding a great new feature to your combo, has provided you some tools for any conversions of snippets you might have to do!


(Print this page)