27

i have the following code in WPA, and i am trying to convert it to WPF. I tried Keydown instead of Keypress and changed, for example,

(e.keyChar == '-') to (e.key == e.Subtract):
  1. its not working the same
  2. I cant find the equal sign within e.key

first code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        foreach (TextBox tb in this.Controls.OfType<TextBox>())
        {
            tb.Enter += textBox_Enter;
        }
    }

    void textBox_Enter(object sender, EventArgs e)
    {
        focusedTextbox = (TextBox)sender;
    }

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;
        }
        else if (e.KeyChar == '-')
        {

            if (focusedTextbox != null)
            {
                if (focusedTextbox.Text == "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    operand1.Real = getOperand.Real;
                    operand1.Imag = getOperand.Imag;
                    flag1 = 2;
                }
            }

        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.KeyChar == '=')
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.ClearSelected();

                    }
                    else
                    operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }

            listBox1.Items.Add(operand1);
        }

    }

second code:

private void win_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Add)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;

        }
        else if (e.Key == Key.Subtract)
        {

            if (textBox2.Text == "")
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                operand1.Real = getOperand.Real;
                operand1.Imag = getOperand.Imag;
                flag1 = 2;
            }

        }
        else if (e.Key == Key.Multiply)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.Key == Key.Divide)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.Key == Key.Enter)
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.UnselectAll();
                    }
                    else
                        operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }


            listBox1.Items.Add(operand1);
        }
    }
Ravi Y
  • 4,296
  • 2
  • 27
  • 38
joseph
  • 353
  • 3
  • 6
  • 10

3 Answers3

33

It's very similar - but you compare e.Key to the Key enumeration.

Register your event handler somewhere (such as the constructor, or window_loaded):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
}

And then in the event handler:

void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Subtract)
    {
        // Do something
    }
}
Will Faithfull
  • 1,868
  • 13
  • 20
  • 1
    i just discovered that nothing is working,,,, i mean . i have 2 texchanged events and the keyDown event,, and only the first textchanged is working , the other are not been executed at all.. – joseph Nov 27 '12 at 16:04
  • 1
    Window_Loaded is the method which is generated for you when you double click on the WPF window in the designer. It runs when the WPF window has finished loading. It sounds like you have retyped the method instead of just registering the event handler in the one that already exists. Try making a brand new WPF application and getting my example to work in there – Will Faithfull Nov 27 '12 at 16:10
  • now im using textbox1_keyDown which have the same code ,,, i did the following private void textBox1_KeyDown(object sender, KeyEventArgs e) { // MessageBox.Show("msg"); if (e.Key == Key.Add) { //MessageBox.Show("msg2 "); operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 1; e.Handled = true; {....} i can see the msg1 whenever i press anything ,, but if i press '+' i dont see msg2 even after closing msg1's message box. – joseph Nov 27 '12 at 16:54
  • could the problem remain within if (e.Key == Key.Add)------ it does the same for all signs not just for + – joseph Nov 27 '12 at 16:56
  • you are using the KeyDown event of "textBox1", rather than the KeyDown event of "MainWindow". Notice how in the above example I registered the event handler to "this.KeyDown". – Will Faithfull Nov 27 '12 at 17:15
  • I found the problem... I don't have a keypad on my computer .. so key.Add is not reading the key when i press "+" or "Shift and -" .. any suggestions on how those keys are represented within the key enumaration?? – joseph Nov 27 '12 at 17:42
16

You're looking for the TextInput event, or perhaps PreviewTextInput.

The EventArgs type is TextCompositionEventArgs; I believe you want the Text property here, but I'm not at all sure of that.

phoog
  • 42,068
  • 6
  • 79
  • 117
7
<TextBox x:Name="txtbx" KeyDown="OnKeyDownHandler" Height="23" Width="250">
</TextBox>
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        txtbx.Text = "You Entered: " + txtbx.Text;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89