Tuesday, November 18, 2008

How to determine the .NET Compact Framework Version on the Device

Here is the best posting I have found to determine the version of the .NET Compact Framework on the device:

http://www.byteswired.com/2007/09/27/how-to-determine-compactframework-version/


The summary is that you look at the registry key:

HKLM\SOFTWARE\Microsoft\.NetCompactFramework

Tuesday, November 11, 2008

Determine OS version with .NET Compact Framework

If you are running a Device application and want to know (in the code) which platform you are running on (it could be CE.NET or Windows Mobile or various versions of each), then you can use:

Environment.OSVersion.Platform

For much more information and several examples, see:

http://www.christec.co.nz/blog/archives/77

Friday, November 7, 2008

Selecting all Items in a ListBox in VB.NET

It seems to me like there should be a method that would select or deselect all items in a listbox, but there is not. Here is a simple sub to accomplish this task:


Public Sub SelectAllListBoxItems(ByVal lv As ListBox, ByVal selected As Boolean)

For i As Integer = 0 To lv.Items.Count - 1

ListBoxValidations.SetSelected(i, selected)

Next

End Sub

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.