KeyPress-Ereignis entspricht in WPF
ich habe den folgenden code in die WPA, und ich bin versucht, es zu konvertieren, um WPF. Ich habe versucht, Keydown statt Keypress und verändert werden, zum Beispiel
(e.keyChar == '-') to (e.key == e.Subtract):
- nicht arbeitet den gleichen
- Ich kann nicht finden, dass das Gleichheitszeichen in e.Schlüssel
ersten 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);
}
}
zweite 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);
}
}
InformationsquelleAutor der Frage joseph | 2012-11-27
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ist es sehr ähnlich - aber Sie vergleichen.e.Der Schlüssel zu der Schlüssel-enumeration.
Registrieren Sie Ihre event-handler-irgendwo (wie der Konstruktor, oder window_loaded):
Und dann in den event-handler:
InformationsquelleAutor der Antwort Will Faithfull
Du suchst die TextInput Ereignis, oder vielleicht PreviewTextInput.
Des EventArgs-Typ ist TextCompositionEventArgs; ich glaube, Sie wollen die
Text
Eigenschaft hier, aber ich bin mir überhaupt nicht sicher.InformationsquelleAutor der Antwort phoog
InformationsquelleAutor der Antwort Salim