Replacing the decimal point key (from numpad) with correct decimal separator (in silverlight!)

silverlight-2.0

What I am trying to do when the user is in a textbox (in silverlight 2.0):

  • When user presses the decimal point
    (.) on the numeric pad, I want to
    have it replaced by the correct
    decimal separator (which is comma
    (,) in a lot of countries)

I can track that the user typed a decimal point by checking in the keydown event

void Cell_KeyDown(object sender, KeyEventArgs e)
{
     if (e.Key == Key.Decimal)

But how do I replace that key with an other in Silverlight. The e.Key is read only. Is there a way to 'send an other key' to the control? Or any other suggestions?

Best Answer

The answer was found on the website of MSDN: Replace-numpad-decimalpoint

Imports System.Threading
Imports System.Windows.Forms

Namespace My


    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator <> "." Then
                System.Windows.Forms.Application.AddMessageFilter(New CommaMessageFilter)

            End If
        End Sub

    End Class

    Friend Class CommaMessageFilter
        Implements IMessageFilter
        Private Const WM_KEYDOWN = &H100

        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage

            If m.Msg = WM_KEYDOWN Then
                Dim toets As Keys = CType(CType(m.WParam.ToInt32 And Keys.KeyCode, Integer), Keys)
                If toets = Keys.Decimal Then
                    SendKeys.Send(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator)
                    Return True
                End If
            End If
            Return False
        End Function
    End Class

End Namespace