Thursday, January 3, 2008

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.

No comments: