Thursday, January 3, 2008

Determine your current Screen Orientation on a Windows Mobile Device

There are many facets of "orientation-aware" apps. There are 2 portrait and 2 landscape modes making 4 possible orientations (0, 90, 180, 270).

I have an application that lets the user set custom keys in an application. At first, I thought I only need to store a landscape set and a portrait set; however, depending on the user, the landscape mode might be in the 90 degree position or the 270 degree position. The keys selected by the user may be very different depending on which one of these orientations gets used.

However, it is very simple to find out what the current screen orientation in on the device:

Imports Microsoft.WindowsCE.Forms
.
.
.
Select Case SystemSettings.ScreenOrientation

Case ScreenOrientation.Angle0
.

.
.
Case ScreenOrientation.Angle90
.

.
.
Case ScreenOrientation.Angle180
.

.
.
Case ScreenOrientation.Angle270
.

.
.
End Select


More Info at:
http://msdn2.microsoft.com/en-us/library/microsoft.windowsce.forms.systemsettings.screenorientation(VS.80).aspx

Detecting Screen Orientation Changes in Windows Mobile Apps

If you are working on a Windows Mobile application, you probably need to be orientation-aware. If the user changes the screen from portrait to landscape or vice versa, you may need to do something based on this change.

The Resize event for the form will tell you when an orientation change has occured. You will probably have to use the me.Width and me.Height to know how it was changed. Look at the If statement in the UpdateButtonLayout sub below.


Private Sub Form_NumberEntry_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

UpdateButtonLayout()

End Sub

Private Sub UpdateButtonLayout()

Dim ratio As Double = CInt(Me.Width) / CDbl(Me.Height)

If ratio <= 0.9 Then ' portrait .
.
.

Else ' landscape

.
.
.
End If

End Sub


Being orientation away for control may be as simple as using the Anchor and Dock properties on the control, but there are many times, that this is not sufficient or that you need to do something else like auto fit columns in a listview.

Wednesday, December 19, 2007

Browsing a .SDF file (SQL Server CE database file)

If you are using SQL Server Compact Editition (SQL Server CE) and are producing .sdf files for use with your applicatoins, it is sometimes desireable to browse through a .sdf file's schema and data. Microsoft provides a nice tool for doing this, the SQL Server Management Studio Express. You must get Service Pack 2 or higher to have the SQL Server CE support.

It will let you select "SQL Server Compact Edition" for your Server Type and you can specify a .sdf file:










You can then browse the database's schema:






And you can perform queries on the data:







You can download the tool from Microsoft:

http://www.microsoft.com/downloads/details.aspx?FamilyID=6053c6f8-82c8-479c-b25b-9aca13141c9e&DisplayLang=en

Friday, December 14, 2007

Initialize char Array in C#

If you are used to initializing a char array in C++ like the following:


char mask[]={'a', 'b', 'c'};



There is a slight change when doing this in C#:

char[] mask=new char[] {'a', 'b', 'c'};


More examples:

http://www.java2s.com/Code/CSharp/Collections-Data-Structure/illustrateshowtoinitializearrays.htm

Wednesday, December 12, 2007

Testing to See if a Camera is Available on Windows Mobile Device

If you are trying to support a built in camera in your Windows Mobile Application, it is often useful to be able to test to see if the camera is availalble in the device. It is simple to do, but maybe not very obvious how to get to this property. Also, if you try to use the CameraCaptureDialog when there is no camera present, it can have unexpected results.

Dim cameraPresent As Boolean = _
CBool(Microsoft.WindowsMobile.Status.SystemProperty.CameraPresent)

Sunday, December 2, 2007

Iterating through a Dictionary Object's Items in VB.NET and C#

Iterating through a Dictionary Object's items may be more complicated than you think; however, with a few examples to go by it turns out to be pretty easy:

VB.NET

Dim DictObj As New Dictionary(Of Integer, String)

DictObj.Add(1, "ABC")
DictObj.Add(2, "DEF")
DictObj.Add(3, "GHI")
DictObj.Add(4, "JKL")

For Each kvp As KeyValuePair(Of Integer, String) In DictObj
Dim v1 As Integer = kvp.Key
Dim v2 As String = kvp.Value
Debug.WriteLine("Key: " + v1.ToString _
+ " Value: " + v2)
Next

C#

Dictionary<int, String> DictObj =
new Dictionary<int, String>();

DictObj.Add(1, "ABC");
DictObj.Add(2, "DEF");
DictObj.Add(3, "GHI");
DictObj.Add(4, "JKL");

foreach (KeyValuePair<int,String> kvp in DictObj)
{
int v1 = kvp.Key;
String v2 = kvp.Value;
Debug.WriteLine("Key: " + v1.ToString() +
" Value: " + v2);
}


It should also be pointed out that you can obtain a collection from the Dictionary Object for both Keys and Values using KeyCollection and ValueCollection:

VB.NET

Dim keyCollection As _
Dictionary(Of Integer, String).KeyCollection = _
DictObj.Keys

For Each key As Integer In keyCollection
Debug.WriteLine("Key: " + key.ToString())
Next

Dim valueCollection As _
Dictionary(Of Integer, String).ValueCollection = _
DictObj.Values

For Each value As String In valueCollection
Debug.WriteLine("Value: " + value.ToString())
Next


C#

Dictionary<int,String>.KeyCollection keyCollection=
DictObj.Keys;

foreach (int key in keyCollection)
{
Debug.WriteLine("Key: " + key.ToString());
}

Dictionary<int,String>.ValueCollection valueCollection = DictObj.Values;

foreach (String value in valueCollection)
{
Debug.WriteLine("Value: " + value.ToString());
}


For more examples see:

http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx

Saturday, December 1, 2007

Debug.WriteLine in C#

If you are used to using Debug.WriteLine in VB.NET, it is sometimes a little confusing when you try to do it in C# and it doesn't work. The trick is to say:


using System.Diagnostics;


at the beginning of the C# app, then the Debug.WriteLine will work as you expect.


You can get more detailed information on this subject from:

http://support.microsoft.com/kb/815788