views:

65

answers:

2

I'm doing a project. I'm not going to details but I will simplify my idea. I'm using Morse Code ( dot and dash) and I have 2 methods: convert_MorseToChar() and Convert_MorseTonum(). In the convert_MorseToChar() method there is a switch to compare the input from a user which will be Morse codes and mapping it to characters:

  private String convert_MorseToChar(ref string Ch) 
  {
    switch (Ch)
        {
        Case ".-":
            MorsetoChar = "a"
            break;
        Case "-...":
            MorsetoChar = "b"
            break;
        Case "-.-.":
            MorsetoChar = "c"
            break;
        Case "-..":
            MorsetoChar = "d"
            break;
        Case ".":
            MorsetoChar = "e"
            break;
        }
   }

and the other method Convert_MorseToNum(), ues the SAME combinations of Morse codes but mapping them to numbers:

  private String Convert_MorseToNum(ref string Ch) 
  {
    switch (Ch)
        {
        Case ".-":
            MorsetoChar = "1"
            break;
        Case "-...":
            MorsetoChar = "2"
            break;
        Case "-.-.":
            MorsetoChar = "3"
            break;
        Case "-..":
            MorsetoChar = "4"
            break;
        Case ".":
            MorsetoChar = "5"
            break;
        }
   }

Now the senario is: there are 2 Textbox, one the user will write Morse codes in it and the other is for the output. The user will write dot . and dash - from the keyboard and press Enter then the program will go to ONE of the 2 methods to convert the Morse codes. Now what tells the program where to go to convert?

my question is: I want to create mode key to switch between 2 modes: MorseToChar and MorseToNum. I want the down arrow key to act like a mode. When a user presses the down arrow then it the program will be in MorseToChar mode, whenever the user input the program directly use the method convert_MorseToChar to convert to characters. When the user press the down arrow again, the program will switch to MorseToNum mode here when ever the user input as morsecode, the program will directly use the method Convert_MorseToNum() to convert to numbers. How can I do that?

Please excuse my English, English is not my native language :)

A: 

You could use a boolean that gets toggled each time the desired key is pressed, like so:

bool NumericMode = false; // accessible to the Morse code methods
private void ToggleMode()
{
    if(NumericMode)
        NumericMode = false;
    else
        NumericMode = true;
}
JYelton
Or: NumericMode = !NumericMode;
Michael Todd
Very concise! Thanks. :)
JYelton
+1  A: 

You can consume the KeyDown event. If the pressed key is the down arrow, you switch modes:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the keystroke is the down arrow.
    if (e.KeyCode == Keys.Down)
    {
        charMode = !charMode;
    }
}

Of course you will have to define your charMode as a bool. And when calling your conversion method, you will check its value.

private String Convert_Morse(ref string Ch) 
{
    if (charMode) return convert_MorseToChar(Ch)
    else
    return convert_MorseToNum(Ch);
}
Paulo Guedes
Thank you! Its what I need but with one difference. How to code this keyboard event so WHERE EVER the focus is, and any time the user is click the down arrow from keyboard the the keydown event will be captured and the boolean variable will toogled? In the same page I have a textbox and associated to it a keyUp event. Thanks!
Sara
I figured out how to do it using:Application.Current.RootVisual.KeyDown += Main_KeyDown;private void Main_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Determine whether the keystroke is the down arrow. if (e.KeyCode == Keys.Down) { charMode = !charMode; } }Cheers..
Sara