Thursday, May 29, 2008

Not xxx Is Nothing and xxx IsNot Nothing in VB.NET

It took me a while to figure that that you can say:

If var1 IsNot Nothing Then
End If

Instead of the awkward:

If Not var1 is Nothing Then
End If

Of course the IsNot is like the Is and will work with when comparing any two object references, not just for testing for Nothing.

Tuesday, May 20, 2008

Finding the "My Documents" folder in

Finding the My Documents folder in VB.NET (or C#) is pretty simple:

Dim dir as String
dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

There are several other special folders you can get as well:
  • Personal (My Documents also work on devices)
  • MyPictures
  • DesktopDirectory
  • MyMusic
  • StartMenu
  • Startup
  • Favorites
  • ApplicationData
  • CommandApplicationData
  • CommonProgramFiles
  • Cookies
  • History
  • InternetCache
  • LocalApplicationData
  • MyComputer
  • ProgramFiles
  • Programs
  • Recent
Here is another article on using SpecialFolders:

Friday, May 16, 2008

StartupPosition for MessageBox

Sometimes it seems that .NET has some obvious shortcomings. For example, all I want to do is have a MessageBox center in my application instead of the screen. This task shouldn't be that difficult; however, there is no way (that I have found) to set the StartPosition property for the MessageBox dialog. While there may be a functional and elegant solution to this problem (please post as a comment if you have one), the only solution I have found on the internet is to implement your own MessageBox class, and code it to do what you want. This isn't hard, but it doesn't seem like a lot of work for something so simple. Also, the Messagebox probably does more than you might first think. It does a lot of autosizing to hold whatever text you provide it.

Here is one implementation of a custom MessageBox I have found:

I am sure there are more such implementations. Please post comments if you know of any.

Wednesday, May 14, 2008

Finding VB.NET or C# Signatures for Interop/PInvoke

There are several places to find the Signatures in VB.NET and C# for Win32Api calls online. Here is one that is pretty good:

Defaulting to Thumbnail View when using OpenFileDialog

You would think that it would be very simple to default an OpenFileDialog to any view type you want via its properties. However, there is no property exposed for setting the view type. I simply want to browse for images with my OpenFileDialog instance, and I would prefer for the user not to have to manually set the view type to Thumbnail everytime.

I found a couple of solutions: (C#) (same as above only in VB.NET)

and the following offers even more functionality:

If someone else has found or created a better solution, please let me know.

Monday, May 12, 2008

Converting VB.NET to C# and C# to VB.NET

Here is a web site that will quickly converting VB.NET to C# or C# to VB.NET:

It is not perfect, but it does all of the grunt work and it is free.

Thursday, May 8, 2008

Keeping a VB.NET Form Always on Top

In VB6, you used a Win32API call to SetWindowPos to get a form to always stay on top. This has been simplified somewhat in VB.NET. Put the following in Form_Load or other appropriate place:

Me.TopMost = True