views:

1389

answers:

5

Hi,

What I am trying todo 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?

Regards,

Tjipke

A: 

You could do your changes in _LostFocus wether it's changing the dot to a comma, or applying the correct culture to the input.

It wouldn't do the change straight away as you desire, but would do it as soon as the user left the text box.

DeletedAccount
Thanks for the suggestion, but from a users pov is that not very clear: he sees 1.5 until he leaves the tb and than it becomes 1,5...
Tjipke
A: 

You could probably read the 'key up' event instead of 'key down' and replace the entire content of the text box with your (entire) preferred substituted string on every keypress.

However, I think you're going about this the wrong way. There's a ',' key on the keyboard. I would assume that if your user meant to type in a ',' because that's the standard in his country, then that's what he would hit. Key substitution on screen would just be confusing.

Alterlife
Thanks for your first suggestion... I will see what I can do there. But this is not confusing for users, for fast number typing they expect the numeric pad '.' to be the local decimal separator. That is also what excel does for example! So to be clear I am only talking about the . on the numpad
Tjipke
A: 

Using Alterlife's answer as a hint for replacing contents, I have the following working hack... But I don't like it :-(.

  • It means that the Text property is set twice, once to the wrong value and then replaced by the right value
  • It only works for text boxes
  • It feels like a hack that someday might just stop working

So suggestions for better, more generic, solutions are very welcome!

The hack:

    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
                }
            }
        }
    }
Tjipke
A: 

You're trying to override your system's locale settings, which may confuse your users when they are getting a wrong response from the key they typed in... What you can do is write a value converter, which would ensure the value will be properly formed before written to the bound data field. Assuming your textbox is bound to the underlying data field...

AndrejT
Thanks for your answer, but no I am not trying to override the system's locale setting (at least I think not). I am trying to let the decimal point on the numpad being used as the DecimalSeparator. Which is what most (data entry) users expect.
Tjipke
That's what Excel does since many years for french users...
Loic
A: 

It's working:

<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>
Savvy
In silverlight?
Tjipke