Replacing decimal point (from numpad) with correct decimal point (in silverlight!)
What I'm trying to do when the user is in the textbox (in silverlight 2.0):
- When the user presses the decimal point (.) On the numeric keypad , I want to replace it with the correct decimal separator (which is the comma (,) in many countries)
I can track that the user has typed a decimal point by checking the keydown event
void Cell_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Decimal)
But how to replace this key with another one in Silverlight. e.Key
is read-only. Is there a way to "send another key" to the control? Or any other suggestions?
source to share
Using Alterlife's answer as a hint to replace content, I have the following working hack ... But I don't like it: - (.
- This means that the Text property is set twice, once to the wrong value and then replaced with the correct value.
- It only works for text fields
- It's like a hack that might just stop working someday.
Therefore, suggestions for better, more versatile solutions are highly welcome!
Breaking:
void CellText_KeyUp(object sender, KeyEventArgs e)
{
var DecSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
if (e.Key == Key.Decimal && DecSep != ".")
{
if (e.OriginalSource is TextBox)
{
var TB = (TextBox)e.OriginalSource;
string sText = TB.Text;
int iPos = TB.SelectionStart - 1;
if (iPos >= 0)
{
System.Diagnostics.Debug.Assert(sText.Substring(iPos, 1) == ".");
TB.Text = sText.Substring(0, iPos) + DecSep + sText.Substring(iPos + 1);
TB.SelectionStart = iPos + 1; // reposition cursor
}
}
}
}
source to share
The answer was found on the MSDN website: 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
source to share
public class NumericUpDown : System.Windows.Controls.NumericUpDown
{
[DebuggerStepThrough]
protected override double ParseValue(string text)
{
text = text.Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
return base.ParseValue(text);
}
}
Greetings
source to share
I have a solution for this which I am using in my winforms apps, suppose it should work in silverlight too. It's in vb, but converting it to C # shouldn't be a problem. Take a look here: http://code.msdn.microsoft.com/Replace-numpad-decimalpoint-c1e0e6cd
source to share
Perhaps you could read the "key up" event instead of "key down" and replace the entire contents of the textbox with your (all) preferred replaced string on each key press.
However, I think you are going this wrong. There is a "," key on the keyboard. I would assume that if your user wanted to enter "," because this is the standard in his country, then this is what he hit. Replacing the keys on the screen is just confusing.
source to share
You are trying to override your system settings, which can confuse your users when they receive an incorrect response from an entered key ... What you can do is write a value converter that will ensure the correct value is formed before writing to the associated data field. Assuming your textbox is associated with an underlying data field ...
source to share
Works:
<html>
<heaD>
<script language="javascript">
function keypress1 ()
{
var e=window.event || e
unicode = e.charCode ? e.charCode : e.keyCode;
if (unicode==46)
{ return (e.charCode ? e.charCode=44 : e.keyCode=44); }
}
function keypress2 ()
{
var e=window.event || e
unicode = e.charCode ? e.charCode : e.keyCode;
if (unicode==46)
{ return (e.charCode ? e.charCode=46 : e.keyCode=46); }
}
function keyDown(e){
if (!e){
e = event
}
var code=e.keyCode;
if(code==110)
return document.onkeypress=keypress1
else if(code=188)
{ document.onkeypress=keypress2 }
}
document.onkeydown = keyDown
</script>
</head>
<body>
<input type=text>
</body>
</html>
source to share