xaml - C# Visual Studio Calculator App Crash when using "Enter" Key -
i trying write graphical calculator, far calculator works, want use key "enter" perform operation , give answer. crashes whenever press enter
. xaml code:
<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">
here code handles enter
key event.
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); } }
i beginner in c#, xaml , visual studio. right way of handling enter.key event?
this exception thrown:
exception thrown: 'system.formatexception' in mscorlib.ni.dll
change this:
double num2 = convert.todouble(tb.text.substring(iop + 1, tb.text.length - iop - 2));
to this:
double num2 = convert.todouble(tb.text.substring(iop + 1, tb.text.length - iop - 1));
Comments
Post a Comment