Saturday, October 25, 2008

Parsing text Syntax Highlighting in a Rich Text Box

Here is an interesting posting on how to format text in a Rich Text Box control so that it does Syntax Highlighing:

http://www.c-sharpcorner.com/uploadfile/duncanharris/syntaxhighlightinrichtextboxp112012005050840am/syntaxhighlightinrichtextboxp1.aspx

Friday, October 24, 2008

Great Info on Working with Text in .NET

The following link shows how to Align Text, Clip Text, Wrap Text, Transform Text, and Shade Text in .NET:


http://www.devx.com/dotnet/Article/33464/1954

Converting a TrueType Font Character into a Bitmap

Here is an excellent article on creating a bitmap from a TrueType Font Character. If you want to create a symbol selection dialog with a ListView, these routines can generate the Bitmap image for the listview items.

http://www.codeproject.com/KB/GDI-plus/TTF_to_bitmap_convert.aspx


This article did more than I needed. I only wanted to create the images for a ListView for all of the characters in a TrueType font. I have boiled down the code from the above article to the following:


Private Sub UpdateSymbolList(fontName as String)

ListView1.Items.Clear()


For i As Integer = 0 To 255

Dim bm As Bitmap = ConvertChar(fontName, i, Color.Blue, 32, 32, 20)
ImageList1.Images.Add(i.ToString, bm)

Next


ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1

For i As Integer = 0 To 255
Dim lvi As New ListViewItem
lvi.Text = i.ToString
lvi.ImageKey = i.ToString
ListView1.Items.Add(lvi)
Next


End Sub

Public Shared Function ConvertChar(ByVal fontName As String, _
ByVal charCode As Integer, _
ByVal color As Color, _
ByVal bitmapWidth As Integer, _
ByVal bitmapHeight As Integer, _
ByVal characterSize As Single) As Bitmap


Dim charToDraw As Char = Chr(charCode)
Dim bitmap As New Bitmap(bitmapWidth, bitmapHeight)
Dim g As Graphics = Graphics.FromImage(bitmap)
Dim fnt As Font = New Font(fontName, characterSize, FontStyle.Regular)
Dim textFormat As New StringFormat()


textFormat.Alignment = StringAlignment.Center
textFormat.LineAlignment = StringAlignment.Center

g.DrawString(charToDraw.ToString, fnt, New SolidBrush(color), New RectangleF(0, 1, bitmapWidth, bitmapHeight), textFormat)

g.DrawRectangle(New Pen(Drawing.Color.Black, 1), New Rectangle(0, 0, bitmapWidth - 1, bitmapHeight - 1))

Return bitmap

End Function

Tuesday, October 21, 2008

Option Strict On causes Disallows Late Binding Error when getting Variant Array from a COM Object

I have several ActiveX controls that I use with .NET. Generally, there are not too many problems using them with .NET, but I have come across one error that was difficult (for me) to solve.

Since the ActiveX controls were originally written for VB6, they return Variants and arrays are returned as Variants of Variants. This is all fine because .NET has an Object type to work with the variants. As a rule, however, I use Option Strict On because if you don't use this option, Dotfuscator will really cause problems without letting you know in advance. But the catch is that with Option Strict on, you cannot do something like the following:

Dim varArray as Object
Dim count as Integer

SomeComObject.GetList( varArray, count )

For i as Integer = 0 to count - 1

Debug.WriteLine( varArray(i).ToString )

Next

varArray will return list of integers, but as a Variant Array of Variant Integer values.

The code above will not work with Option Strict On because the varArray(i) is late bound and this is not allowed. My first though was to try:

Dim val as integer = CType( varArray(i), Integer)

The above code does not work or any similar cast.

The only solution I have discovered (and there are probably others) is to cast the Variant array as an Object array like:

Dim vArray() as Object = CType( varArray, Object())

Then the vArray is a Object Array and can be further cast to other types without catching on the Option Strict On. For example:

Dim catList As Object = Nothing
Dim offsetList As Object = Nothing
Dim filterList As Object = Nothing


While viewObj.GetElementsInRangeFetch(1000, catList, offsetList, filterList, blockCount)

Dim cList() As Object = CType(catList, Object())
Dim oList() As Object = CType(offsetList, Object())
Dim fList() As Object = CType(filterList, Object())

For j As Integer = 0 To blockCount - 1
ProcessFeature(CInt(cList(j)), CInt(oList(j)), CInt(fList(j)))
Next

catList = Nothing
offsetList = Nothing
filterList = Nothing

End While


This may not be the cleanest solution, but it does work and allow the Option Strict setting to be left on.

Option Strict On causes Disallows Late Binding Error when getting Variant Array from a COM Object

I have several ActiveX controls that I use with .NET. Generally, there are not too many problems using them with .NET, but I have come across one error that was difficult (for me) to solve.

Since the ActiveX controls were originally written for VB6, they return Variants and arrays are returned as Variants of Variants. This is all fine because .NET has an Object type to work with the variants. As a rule, however, I use Option Strict On because if you don't use this option, Dotfuscator will really cause problems without letting you know in advance. But the catch is that with Option Strict on, you cannot do something like the following:

Dim varArray as Object
Dim count as Integer

SomeComObject.GetList( varArray, count )

For i as Integer = 0 to count - 1

Debug.WriteLine( varArray(i).ToString )

Next

varArray will return list of integers, but as a Variant Array of Variant Integer values.

The code above will not work with Option Strict On because the varArray(i) is late bound and this is not allowed. My first though was to try:

Dim val as integer = CType( varArray(i), Integer)

This doesn't work or any similar cast.

The only way solution (and there are probably others) is to cast the Variant array as an Integer array like:

Dim vArray() as Integer = CType( varArray, Integer())

Then the vArray is a regular Integer Array.

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