0

In Silverlight project, how to make the left arrow act like . (dot), when a user press the left arrow in a textbox it will type . and also in the same way how to make the right arrow act like - ( dash)

And I want to use the CTRL key to switch between 2 modes: . and dash, regular arrows behavior, mean when a user press Control the tow arrows will act as . and dash. And when a user press agian the control the 2 arrows will act as usual arrows.

Sara
  • 21
  • 3
  • 2
    Is this in Windows Forms or ASP.NET? And what kind of control? (Or something else?) – Jesse Millikan Apr 11 '10 at 09:01
  • Perhaps it's not about inside an app, but system wide, using C# to do the implementation. @le.shep20, can you update your question to clarify what you're after? – Abel Apr 11 '10 at 09:30
  • OH i'm sorry I should be more clear..actually I'm working on Silverlight project, how can accomplish this task in Silverlight? – Sara Apr 11 '10 at 10:49

2 Answers2

2

If it's win forms or WPF you just catch the event of the keypressed and change it's behavior and than set it as "Handled" (there is a bunch of events before and after (PreviewKeyDown) that you can use to fully control what happens on every key pressing.

You can check if CTRL key is pressed as well using API. using KeyboardDevice Property in WPF, checking:

if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)

Addition: Meanwhile - check this out SO question

and This one as well: SO Question2

Community
  • 1
  • 1
Dani
  • 14,639
  • 11
  • 62
  • 110
  • Thank you Dani, but I forgot to mention that I'm working on silverlight application! how I can do it with Silverlight- I'm new to it! – Sara Apr 11 '10 at 10:53
  • I don't know silverligt but I know it is very very similar to wpf. I will check and try to update the answer. – Dani Apr 11 '10 at 11:24
0
private void textBox1_KeyUp(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) 
                    { 
                        textBox1.SelectionStart = textBox1.Text.Length + 1; 
                        insert = '.';
                    }
                    else
                    { 
                        insert = '-';
                    } 
                    int i = textBox.SelectionStart;
                    textBox1.Text = textBox1.Text.Insert(i, insert.ToString());
                    textBox1.Select(i + 1, 0);
                }
            }
        }
Sara
  • 21
  • 3