使用“Enter”键

时间:2015-08-30 19:31:46

标签: c# xaml visual-studio-2015

我正在尝试编写一个图形计算器,到目前为止计算器正常工作,但我想使用键“Enter”来执行操作并给出答案。现在只要按Enter,它就会崩溃。这是XAML代码:

<TextBox Height="38" TextWrapping="Wrap" VerticalAlignment="Top"
 Margin="16,23,0,0" HorizontalAlignment="Left" Width="226" 
 FontSize="20" Background="Black" BorderThickness="2" Name="tb"
 Text="" KeyUp="KeyDownHandler">

以下是处理Enter键事件的代码。

private void KeyDownHandler(object sender, KeyRoutedEventArgs e)
{
     if (e.Key == Windows.System.VirtualKey.Enter)
     {
          result();
     }
}     

private void result()
{
     String op;
     int iOp = 0;
     if (tb.Text.Contains("+"))
     {
         iOp = tb.Text.IndexOf("+");
     }
     else if (tb.Text.Contains("-"))
     {
         iOp = tb.Text.IndexOf("-");
     }
     else if (tb.Text.Contains("*"))
     {
         iOp = tb.Text.IndexOf("*");
     }
     else if (tb.Text.Contains("/"))
     {
         iOp = tb.Text.IndexOf("/");
     }
     else
     {
         //error
     }

     op = tb.Text.Substring(iOp, 1);
     double num1 = Convert.ToDouble(tb.Text.Substring(0, iOp));
     double num2 = Convert.ToDouble(tb.Text.Substring(iOp + 1, tb.Text.Length - iOp - 2));

     if (op == "+")
     {
         tb.Text = Convert.ToString(num1 + num2);
     }
     else if (op == "-")
     {
         tb.Text = Convert.ToString(num1 - num2);
     }
     else if (op == "*")
     {
         tb.Text = Convert.ToString(num1 * num2);
     }
     else if (op == "/")
     {
         tb.Text = Convert.ToString(num1 / num2);
     }
}

我是C#,XAML和Visual Studio的初学者。这是处理Enter.key事件的正确方法吗?

这是抛出的异常:

  

抛出异常:mscorlib.ni.dll中的“System.FormatException”

1 个答案:

答案 0 :(得分:1)

改变这个:

process2

对此:

import sys
from kafka.client import KafkaClient
from kafka import SimpleConsumer
import time
from multiprocessing import Process,Queue

def kafka_init():
    client=KafkaClient('kafka1.wpit.nile.works')
    consumer=SimpleConsumer(client, "druidkafkaconsumer", "personalization.targeting.clickstream.prod")
    return consumer

def consumeMessages(q):
    print "thread started"
    while not q.empty():
        try:
            print q.get(True,1)
        Queue.Empty:
            break
    print "thread ended"
if __name__=="__main__":
    starttime=time.time()
    timeout=starttime+ 10 #timeout of read in seconds
    consumer=kafka_init()
    q=Queue()
    p=Process(target=consumeMessages,args=q)
    while(True):
        q.put(consumer.get_message())
        if time.time()>timeout:
            #transfer logic from process1 to main process here.
            print "Start time",starttime
            print "End time",time.time()
            p.start()
            p.join()
            break