problem with converting simple code from Winform to silverlight app.

Posted by Sara on Stack Overflow See other posts from Stack Overflow or by Sara
Published on 2010-04-17T23:13:47Z Indexed on 2010/04/17 23:33 UTC
Read the original article Hit count: 212

Filed under:
|
|

Hi. I have this code for window form application and I have been attempting to convert it to a Silverlight application but it does not work!. There is a Textbox and I attached KeyDown event handler to it. when the user press the arrow key ( left or right) while the focus on the textbox, it will write . or -. When it is window form i used e.KeyCode and Keys.Right and its works great but when it is silverlight I used e.Key and key.Right and the program doesn't work good because the arrows do the 2 functions moving and write ./-. How I can work this out in Silverlight? (My English not good)

The code ( window form):

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox textBox = (TextBox)sender; 
                if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                {
                    e.Handled = true; 
                    char insert; 
                    if (e.KeyCode == Keys.Left) 
                    { insert = '.'; }

                    else
                    { insert = '-'; } 
                    int i = textBox.SelectionStart; 
                    textBox.Text = textBox.Text.Insert(i, insert.ToString()); 
                    textBox.Select(i + 1, 0);
                }
            }
        }

(and Silverlight):

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox textBox = (TextBox)sender; 
                if (e.Key == Key.Left || e.Key == Key.Right)
                {
                    e.Handled = true; 
                    char insert;
                    if (e.Key == Key.Left) 
                    { insert = '.'; }

                    else
                    { insert = '-'; } 
                    int i = textBox.SelectionStart; 
                    textBox.Text = textBox.Text.Insert(i, insert.ToString()); 
                    textBox.Select(i + 1, 0);
                }
            }
        }

I don't understand, is there huge different effect between using Keycode/Keys and Key/Key or because something else?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about winforms