Code Repo    |     RSS
MD's Technical Sharing



Sunday, August 15, 2010

Allowing only numeric input in a TextBox

You may have seen a Windows textbox that would disallow non-numeric input, showing an error message like in the following screenshot:

This can be easily accomplished from .NET by applying the ES_NUMBER style to the textbox:

Public Sub SetNumericInputMode(ByVal txtBox As TextBox)
    Dim style As Int32 = GetWindowLong(txtBox.Handle, GWL_STYLE)
    SetWindowLong(txtBox.Handle, GWL_STYLE, style Or ES_NUMBER)
End Sub

Of course, SetWindowLong has to be declared properly:

<DllImport("user32.dll")>
Private Shared Function SetWindowLong( _
     ByVal hWnd As IntPtr, _
     ByVal nIndex As Integer, _
     ByVal dwNewLong As IntPtr) As Integer
End Function

Now what if you're using the RadTextBox control from the Telerik RadControls for WinForms? The following code works for a normal textbox, compiles but will not work for a RadTextBox:

SetNumericInputMode(textBox1.Handle);

The reason is that the RadTextBox is just a container for the native Win32 textbox. Any style changes have to be applied directly to the actual textbox, not the container. The following will work:

Public Sub SetNumericInputMode(ByVal txtBox As RadTextBox)
  'special handling for RadTextbox as the actual Win32 textbox is hidden underneath
  Dim hwnd As IntPtr = CType(txtBox.TextBoxElement.Children(0), RadTextBoxItem).HostedControl.Handle
  Dim style As Int32 = GetWindowLong(hwnd, GWL_STYLE)
  SetWindowLong(hwnd, GWL_STYLE, style Or ES_NUMBER)
End Sub

Notice that ES_NUMBER only prevents user from entering non-numeric (0..9) input for the textbox. It does not stop user from pasting random text. For more advanced features, I suggest something like MaskedTextBox
Read More »

Sunday, August 8, 2010

Migrate SMS from Windows Mobile to Android

To migrate SMS from Windows Mobile to Android, you will need the SMS Exporter tool, which will export all SMS to an XML file that can be imported to Android using SMS Backup Restore

Same as my SMS2IPD tool that migrates SMS from Windows Mobile to Blackberry, SMS Exporter uses MAPIDotNet library found on codeproject, written by rwt33. The export speed on Windows Mobile is fast, but the import speed is very slow. On my HTC Dream flashed to Android 2.1, it took almost 2 hours to import 3000 messages - the process became slower towards the end and the phone would go to standby mode during the process. After the painful import , the message application is still responsive despite the huge message database.

SMS Exporter also supports backing up SMS to XML, which is useful if you want to read your SMS on a computer, or migrate your SMS to other phones.
Read More »