Tuesday, November 22, 2005

List of Invalid Keyboard Characters for a Windows XP filename

What keyboard characters are not valid for a Windows XP filename?

This has come up several times over the years during my programming tasks when I have tried to automatically generate a filename based on some collection of data. It seems a good time to document this relatively simple question for my future reference as well as yours.

The keyboard characters that cannot be used in a Windows XP filename are:

* Asterisk

Pipe (vertical bar)

\ Back slash

/ Forward slash

: Colon

" Quote

< Less Than

> Greater Than

? Question mark


Hope that helps!
Joe Kunk

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

Monday, August 22, 2005

Getting the volume label in .Net

To get the volume label of a particular drive, use the Dir method in the VisualBasic namespace. Note that you can ONLY use the Volume fileattribute if you want to get the volume name. Use of any other attribute will prevent the volume label from being returned. On my system, the DVD drive is my E:\, so the following statement successfully returned the volume label for me:

'Get the CD/DVD volume label
Dim VolumeLabel As String = _
Microsoft.VisualBasic.Dir("E:\", FileAttribute.Volume)


Joe Kunk

Sunday, August 21, 2005

Please feel free to comment on my posts

I just changed the settings on this blog to allow comments by anyone, not just members of blogger.com. I want to encourage comments. If you read one of my posts and have anything to add, please do so. I look forward to the dialog.

Joe Kunk

Using IO.Directory.GetFiles with a filename wildcard

I got an "Illegal characters in path" error on the following statement:
Dim TxtNames As String() = IO.Directory.GetFiles("c:\temp\*.txt")
and from the documentation it was not clear to me why I was getting the error.

Turns out that you must use the second overload version of the GetFiles method with two strings if you want to include a wildcard.

In the example above, the two possible correct versions of the GetFiles method include:

Dim TxtNames As String() = IO.Directory.GetFiles("c:\temp")
Dim TxtNames As String() = IO.Directory.GetFiles("c:\temp","*.txt")

The second statement provides the desired result of returning a string array of all filenames with a txt extension in the c:\temp folder. The first statement returns all files in the c:\temp folder which can be scanned with a loop for further processing.

Note that the single string overload verison of GetFiles can only accept a directory name.

I hope this helps clarify a point that managed to confuse me. As usual, it's obvious once you know the answer!

Joe Kunk

Friday, August 19, 2005

GLUGnet Presentation 08-18-05

Last night I gave the feature presentation to GLUGnet, the Greater Lansing User Group for .Net. The topic was language enhancements and conversion recommendations for Visual Basic 2005. The Powerpoint presentation is available for free download at http://portal.artemis-solutions.com/glugnet/go/iris/3373/en-US/DesktopDefault.aspx. Download the file named, "GLUGnet August 18, 2005.zip".

There was an error on the example for the CONTINUE statement which has been corrected.

The Internet web address for all the sites I mentioned at the GLUGnet meeting were added as the last slide of the presentation.

Thank you to GLUGnet for allowing me to give this presentation. I learned a lot by doing the research for the presentation and I really enjoyed the experience. GLUGnet is a great user group and I'm very proud to be a part of it.

If you find yourself in the East Lansing Michigan area on the third Thursday of the month, please feel free to join us for free food and drinks and an educational meeting. See the web site http://www.glugnet.org for more information.

Joe Kunk

Thursday, August 11, 2005

Two VB.NET blogs you don't want to miss

Today I ran across two blogs about Visual Basic that are awesome resources.

The first is the blog from the Visual Basic .Net IDE team. From their introduction, "This blog is hosted by the program managers, developers, and testers who work on the Visual Basic Integrated Development Environment (VB IDE). " This blog has an awesome FAQ and further tips on the cool new feature of Code Snippets. Other topics include code generation for overrides, a snippet editor, refactoring tool, new debugger features, and an awesome list of the 10 best new features in Visual Basic 2005. It can be found at http://blogs.msdn.com/vbide/default.aspx

The second is "Don't be Iffy" by Julie Lerman. This is a extensive informal blog by a self-employed programmer who clearly loves programming and Visual Basic.Net. Her personality really comes through on her blog. I think you will find it both informative and enjoyable. http://www.thedatafarm.com/blog/

Joe Kunk

Friday, July 22, 2005

When is True not True?

When saving a boolean value to a Microsoft® SQL Server 2000 database. In SQL Server 2000, boolean values are represented as data type "bit", with valid values of numeric 0 or 1. When building a SQL INSERT or UPDATE statement, we need to convert a value of TRUE or FALSE easily to a 1 or 0 respectively.

I have found the easiest way is to use the IIF command. Depending on your coding preferences, you should find one of the two choices below will work well for you:

Dim MySQLBoolean as string = IIF(MyBoolean, "1", "0")
or
Dim MySQLBoolean as integer = IIF(MyBoolean, 1, 0)

Compared to an IF-THEN-ELSE approach, I like the IIF approach since it delivers the desired result in a single line of code.Readabilityy in code is very important, but with all other things being equal, one line of code is generally more readable than five lines of code.

Joe Kunk

Thursday, July 14, 2005

The Power of Panels in ASP.NET

It is fairly common that it takes several pages to be presented to the user to complete a single logical task within a web application.

Say you have a collection of free email newsletters that you want to offer to your site visitors. If a visitor clicks on the link to select one or more newsletters, you may well present:

[Screen 1] a checklist of the newsletters to choose from with a Submit button, then

[Screen 2] a list of just the newsletters selected with a confirmation button, then

[Screen 3] a printer friendly listing of the selected newsletters along with information on how to change or cancel subscriptions and a nice thank you for subscribing to your site newsletters.

Prior to ASP.NET, you likely would have written these as three separate ASP pages; Screen 1 posts to Screen 2 which posts to Screen 3. Easy enough, but it certainly cluttered up the folders on your web server and tracking logic errors was more difficult across three separate source files.

Now with ASP.Net, all three screens can easily be coded into individual panel objects, all within the same .ASPX web page. The code-behind file makes the needed panel visible and sets the visibility of the other panels to false. The user still sees three different screens in sequence but coding is greatly simplified since it is all contained within a single .vb or .cs code-behind file.

I have used this technique in several web applications and it works great. The invisible panels are not included in the final page to the browser so the download times remain tight. I hope you find this tip very useful.

Joe Kunk

Tuesday, July 12, 2005

VB.Net Modules in ASP.NET - Trick or Treat in July

Here is the story of a feature in VB.Net that is at the same time very useful and potentially very dangerous.

Microsoft carried the construct of a Module forward from Visual Basic 6 into VB.Net (reluctantly as I understand it). A popular feature among VB6 programmers, it can be viewed as a global class available to all other forms and classes. VB6 programmers commonly used it to hold global variables and methods that would be needed by multiple forms. No instantiation needed, it was just there and global. Very convenient.

VB6 was used primarily to write single-user desktop windows applications. Yes, with the proper level of skill and prayer it could be used for more advanced purposes, but it truly shined as a way to quickly deliver high-quality desktop applications. In a single user environment, a shared global module is not a problem. Use of a module in a single user VB.Net application is still not a problem for the same reason.

Now consider the impact of a module in an ASP.Net application running on a IIS web server. Herein lies the power and the danger.

First the danger. Like before, a module is a class that automatically global to the entire application without requiring any instantiation. But now that single application is being used simultaneously by potentially hundreds or thousands of users. See the problem yet? Any variable declared at the module level is single-instance shared global among all users. If you casually define a module level variable of 'UserName' and base your security permissions on that, you will experience tremendous security issues as incoming users take on whatever security permissions were enabled by the last client that set the 'UserName' variable. Ouch!

Now the power. Modules are a great way to share single-instance methods for very efficient memory usage in VB.Net web applications. Only one copy of each module method is loaded in memory for all users of the application. As long as each of these modules are designed with shared usage in mind and do not depend on any static variables or try to maintain state across calls, this can be a very powerful and efficient design strategy.

Conclusion: In VB.Net web applications, use module level variables with great care, or not at all if possible. Use module methods with care to ensure that they can be safely shared.

I hope you find this information helpful. I wish I had known it earlier and saved myself some grief. Your comments are welcome.

Disclaimer: This post is based on some painful lessons learned from personal experience and not the result the detailed research on the topic. All of the statements in this post are correct to the best of my knowledge but please verify any use of this information with sufficient testing in your particular environment.

Joe Kunk

C#.Net versus VB.Net

This is one of the first issues a developer will face when making the commitment to begin working with the .Net platform. Which to use as your primary development language?

There may be external factors that will push you toward one choice over the other, such as standards in your workplace, a large legacy codebase of C++ or VB, or even the personal preference of your mentor. Since the .Net framework is shared by all .Net languages, it is essentially a toss-up. There are functional differences between the two languages, but they are minor. The good news is that the consistent .Net framework method names between both languages makes it relatively easy for a VB.Net programmer to read C# code, and for a C# programmer to read VB.Net code.

I prefer to work in VB.Net, simply because I have worked in Visual Basic for years and am very comfortable with that style of coding. That said, I can work in C# with only slightly more effort and I think you would find the same to be true. I have seen several surveys over the last year on this issue and developers are pretty evenly split among the two, with professional developers slightly in favor of C#, probably due to the strong presence of C++ code in commercial software applications.

If you have the freedom to make this choice yourself, then choose the language that you are most comfortable in and know that you are in good company with either choice. Learning to be productive in .Net application development is challenging enough, there is no need to complicate it with unnecessary worry about which language to work in.

Joe Kunk

Monday, July 11, 2005

Welcome to JBK.Net

The goal of this BLOG is to share my experiences as a professional application developer and as a board member for our local .Net developer user group.

I have been programming since 1978 and have watched application development evolve from very humble beginnings to the depth & breadth of what Microsoft's .Net environment has to offer today. There is intense joy and pride in delivering an elegant application to the user and knowing that it is among the best software available anywhere for that purpose. I felt that joy with my first program in 1978 and I still enjoy that feeling every time I deliver a new application. That feeling, at least in part, is what drives me to be the best programmer that I can be, and continue to strive to deliver an ever more exciting application next time.

I firmly believe that the .Net platform is the most productive application development platform available today. By taking advantage of its rich framework, I can create applications in a reasonable timeframe that I would never have even attempted before. It has expanded my horizons as a developer immensely. It is an exciting time in the application development world.

By sharing my experiences with .Net, both good and bad, I hope to help others become more productive and effective with .Net.

I will share my mistakes so that others can avoid them. I will share my questions when I am struggling, so we can learn together. I will share my successes so that others can add to their knowledge and skills.
Like most developers, what I don't know about .Net far exceeds what I know. I expect to learn a lot from the feedback and comments of the readers of this blog and I truly look forward to the dialog.

Welcome to JBK.Net. I appreciate your company and companionship on the shared quest to become skilled and productive .Net developers.

Joe Kunk