Sunday, October 19, 2008

Auto Fit Columns in a DataGridView

I have posted a function to auto-fit the columns in a ListView before. However, when I wanted to do the same thing for a DataGridView, the task is a little different (yet easier):


DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

Thursday, October 16, 2008

Basic Registry Operations with VB.NET

Reading and Writing to the Registry in VB.NET is fairly simple, especially when compared to using C++. However, it can still be a little difficult to get going. Here are 3 functions to perform the basic Registry operations: Read, Write, and Delete.

Examples Using the Functions:

Dim value As String = _
GetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\CompanyName\ApplicationName", "License")


SetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\CompanyName\ApplicationName", _
"License", "value")

DeleteRegistryValue(Registry.LocalMachine, _
"SOFTWARE\CompanyName\ApplicationName", "License")


Read a Registry Value

Public Function GetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As String

Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey

registrySubKey = registryKey.OpenSubKey(subKey)

If registrySubKey IsNot Nothing Then
Try
value = registrySubKey.GetValue(valueName).ToString
Catch ex As Exception
value = ""
End Try
registrySubKey.Close()
End If

Return value

End Function

Write a Registry Value

Public Function SetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String, ByVal value As String) As Boolean

Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey

registrySubKey = registryKey.OpenSubKey(subKey, True)

If registrySubKey Is Nothing Then
registrySubKey = registryKey.CreateSubKey(subKey, RegistryKeyPermissionCheck.ReadWriteSubTree)
End If

If registrySubKey IsNot Nothing Then
registrySubKey.SetValue(valueName, value)
registrySubKey.Close()
Return True
End If

Return False

End Function


Delete a Registry Value

Public Function DeleteRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As Boolean

Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey

registrySubKey = registryKey.OpenSubKey(subKey, True)

If registrySubKey IsNot Nothing Then
Dim retValue As Boolean = True
Try
registrySubKey.DeleteValue(valueName)
Catch ex As Exception
retValue = False
End Try
registrySubKey.Close()
Return retValue
End If

Return False

End Function

Friday, October 3, 2008

FTP Upload and Download in VB.NET and C#

The following site has C# code to upload and download a file via FTP in C#:

http://www.codeguru.com/csharp/csharp/cs_internet/desktopapplications/article.php/c13163


You can take the C# code and paste into the converter at the following web site to get VB.NET:

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Thursday, October 2, 2008

Getting Windows Mobile Owner Name and Device Name

It is sometimes useful to get the Owner Name and/or the Device Name of the Windows Mobile device. If you are wanting a Device Id, then see this posting.

There are different ways to get the Owner Name and Device Name:

For Owner Name, add the following reference to your project:

Microsoft.WindowsMobileMicrosoft.WindowsMobile.Status

Then in your code:

Imports Microsoft.WindowsMobile.Status
.
.
.
MessageBox.Show(SystemState.OwnerName)

More info at:

http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=46306



To get the Device Name (which is what you find under Start/Settings/System Tab/About), you simply have to say:

MessageBox.Show(System.Net.Dns.GetHostName())

For more Info:

http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx

Wednesday, September 24, 2008

Unresolved Externals when using POOM in VC++

I found several postings when searching for a solution to compiling a POOM app that errors out with Unresolved External Symbol CLSID_Application and IID_IPOutlookApp. Only one of the solutions I found worked for me using Visual Studio 2008 C++:

http://www.tech-archive.net/Archive/PocketPC/microsoft.public.pocketpc.developer/2006-03/msg00236.html


The trick is to use:

#include "initguid.h"
#include "pimstore.h"

at the begining of the .cpp file. The initguid.h is the item that fixes the problem.

Saturday, September 6, 2008

Incrementing FileVersion for VC++

In a recent posting, I talked about solutions to incrementing the FileVersion in Visual Studio .NET for VB.NET and C# project. This need was motivated by the changes in VS2008 that requires a FileVersion to be changed before the Windows Installer will replace a file.

Some of my projects have Native DLLs that are compiled in VS 2008's VC++ compiler. These files has the same problem with the installer, yet the previous solution (http://dotnetref.blogspot.com/2008/09/auto-incrementing-file-version-in.html) did not address.

I found this very handy Add-In that works well with VS2008 (and previous versions) that will increment the FileVersion (Product Version, etc.) in the VC++ projects resource file.

http://www.codeguru.com/cpp/v-s/devstudio_macros/visualstudionet/article.php/c14981

Friday, September 5, 2008

Visual Studio 2008 Setup Project always requiring .NET Framework 3.5 even when 2.0 is Targeted

I am slowly upgrading my projects from VS2005 to VS2008. I had a puzzling problem with the Setup Projects always requiring the .NET Framework 3.5 even when the Setup Project is targeted to 2.0 and the application it is delivering is using .NET Framework 2.0.

There are several posting on the the internet about solving this problem. It basically requires you to go to View/Editor/Launch Condition and change the .NET Framework to the version you desire. The Project properties for your setup project are a bit misleading when they have no affect on this problem.

Read these posting for more detailed explanations:

http://impressionsoft.blogspot.com/2008/03/visual-studio-2008-setup-project-and.html

http://blogs.msdn.com/cschotte/archive/2007/08/15/vs2008-launch-conditions-and-prerequisites.aspx