c# - What am I doing wrong on the decimal button, seems my bool is off? -
i not understand doing incorrectly in dotbutton, shows not give correct results? advise?
public partial class form1 : form { public form1() { initializecomponent(); } private bool isfirst = true; private bool shouldclear = true; private double num1; private double num2; private double result = 0; private string symbol = string.empty; private bool hasdecimal = false; private void numberbutton_click(object sender, eventargs e) { button sourcebutton = (sender button); double oldnumber, buttonnumber, newnumber; if (shouldclear) { outputtextbox.clear(); oldnumber = 0; shouldclear = false; } else { oldnumber = double.parse(outputtextbox.text); hasdecimal = true; } buttonnumber = double.parse(sourcebutton.text); newnumber = (oldnumber * 10) + buttonnumber; if (isfirst) { num1 = newnumber; } else { num2 = newnumber; } outputtextbox.text += sourcebutton.text; calculate(symbol); } private void calculate(string operatorsymbol) { if (isfirst) { result = num1; return; } switch (operatorsymbol) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } } private void operatorbutton_click(object sender, eventargs e) { num1 = result; button sourcebutton = (sender button); string operatorsymbol = sourcebutton.text; if (isfirst) isfirst = false; hasdecimal = true; shouldclear = true; symbol = operatorsymbol; outputtextbox.text = result.tostring(); } private void equalsbutton_click(object sender, eventargs e) { outputtextbox.text = result.tostring(); isfirst = true; shouldclear = true; hasdecimal = true; } private void clearbutton_click(object sender, eventargs e) { outputtextbox.text = "0"; result = 0; isfirst = true; shouldclear = true; hasdecimal = false; } private void dotbutton_click(object sender, eventargs e) { if (hasdecimal) hasdecimal = true; isfirst = false; shouldclear = true; outputtextbox.text = dotbutton.text; } } } namespace windowsformsapplication2 { partial class form1 { /// <summary> /// required designer variable. /// </summary> private system.componentmodel.icontainer components = null; /// <summary> /// clean resources being used. /// </summary> /// <param name="disposing">true if managed resources should disposed; otherwise, false.</param> protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } #region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> private void initializecomponent() { this.zerobutton = new system.windows.forms.button(); this.onebutton = new system.windows.forms.button(); this.twobutton = new system.windows.forms.button(); this.threebutton = new system.windows.forms.button(); this.fourbutton = new system.windows.forms.button(); this.fivebutton = new system.windows.forms.button(); this.sixbutton = new system.windows.forms.button(); this.sevenbutton = new system.windows.forms.button(); this.eightbutton = new system.windows.forms.button(); this.ninebutton = new system.windows.forms.button(); this.dotbutton = new system.windows.forms.button(); this.addbutton = new system.windows.forms.button(); this.subtractbutton = new system.windows.forms.button(); this.multiplybutton = new system.windows.forms.button(); this.dividebutton = new system.windows.forms.button(); this.clearbutton = new system.windows.forms.button(); this.equalsbutton = new system.windows.forms.button(); this.outputtextbox = new system.windows.forms.textbox(); this.suspendlayout(); // // zerobutton // this.zerobutton.location = new system.drawing.point(9, 230); this.zerobutton.margin = new system.windows.forms.padding(2); this.zerobutton.name = "zerobutton"; this.zerobutton.size = new system.drawing.size(58, 41); this.zerobutton.tabindex = 0; this.zerobutton.text = "0"; this.zerobutton.usevisualstylebackcolor = true; this.zerobutton.click += new system.eventhandler(this.numberbutton_click); // // onebutton // this.onebutton.location = new system.drawing.point(9, 184); this.onebutton.margin = new system.windows.forms.padding(2); this.onebutton.name = "onebutton"; this.onebutton.size = new system.drawing.size(38, 41); this.onebutton.tabindex = 1; this.onebutton.text = "1"; this.onebutton.usevisualstylebackcolor = true; this.onebutton.click += new system.eventhandler(this.numberbutton_click); // // twobutton // this.twobutton.location = new system.drawing.point(51, 184); this.twobutton.margin = new system.windows.forms.padding(2); this.twobutton.name = "twobutton"; this.twobutton.size = new system.drawing.size(38, 41); this.twobutton.tabindex = 2; this.twobutton.text = "2"; this.twobutton.usevisualstylebackcolor = true; this.twobutton.click += new system.eventhandler(this.numberbutton_click); // // threebutton // this.threebutton.location = new system.drawing.point(93, 184); this.threebutton.margin = new system.windows.forms.padding(2); this.threebutton.name = "threebutton"; this.threebutton.size = new system.drawing.size(38, 41); this.threebutton.tabindex = 3; this.threebutton.text = "3"; this.threebutton.usevisualstylebackcolor = true; this.threebutton.click += new system.eventhandler(this.numberbutton_click); // // fourbutton // this.fourbutton.location = new system.drawing.point(9, 139); this.fourbutton.margin = new system.windows.forms.padding(2); this.fourbutton.name = "fourbutton"; this.fourbutton.size = new system.drawing.size(38, 41); this.fourbutton.tabindex = 4; this.fourbutton.text = "4"; this.fourbutton.usevisualstylebackcolor = true; this.fourbutton.click += new system.eventhandler(this.numberbutton_click); // // fivebutton // this.fivebutton.location = new system.drawing.point(51, 139); this.fivebutton.margin = new system.windows.forms.padding(2); this.fivebutton.name = "fivebutton"; this.fivebutton.size = new system.drawing.size(38, 41); this.fivebutton.tabindex = 5; this.fivebutton.text = "5"; this.fivebutton.usevisualstylebackcolor = true; this.fivebutton.click += new system.eventhandler(this.numberbutton_click); // // sixbutton // this.sixbutton.location = new system.drawing.point(93, 139); this.sixbutton.margin = new system.windows.forms.padding(2); this.sixbutton.name = "sixbutton"; this.sixbutton.size = new system.drawing.size(38, 41); this.sixbutton.tabindex = 6; this.sixbutton.text = "6"; this.sixbutton.usevisualstylebackcolor = true; this.sixbutton.click += new system.eventhandler(this.numberbutton_click); // // sevenbutton // this.sevenbutton.location = new system.drawing.point(9, 93); this.sevenbutton.margin = new system.windows.forms.padding(2); this.sevenbutton.name = "sevenbutton"; this.sevenbutton.size = new system.drawing.size(38, 41); this.sevenbutton.tabindex = 7; this.sevenbutton.text = "7"; this.sevenbutton.usevisualstylebackcolor = true; this.sevenbutton.click += new system.eventhandler(this.numberbutton_click); // // eightbutton // this.eightbutton.location = new system.drawing.point(51, 93); this.eightbutton.margin = new system.windows.forms.padding(2); this.eightbutton.name = "eightbutton"; this.eightbutton.size = new system.drawing.size(38, 41); this.eightbutton.tabindex = 8; this.eightbutton.text = "8"; this.eightbutton.usevisualstylebackcolor = true; this.eightbutton.click += new system.eventhandler(this.numberbutton_click); // // ninebutton // this.ninebutton.location = new system.drawing.point(93, 93); this.ninebutton.margin = new system.windows.forms.padding(2); this.ninebutton.name = "ninebutton"; this.ninebutton.size = new system.drawing.size(38, 41); this.ninebutton.tabindex = 9; this.ninebutton.text = "9"; this.ninebutton.usevisualstylebackcolor = true; this.ninebutton.click += new system.eventhandler(this.numberbutton_click); // // dotbutton // this.dotbutton.location = new system.drawing.point(72, 230); this.dotbutton.margin = new system.windows.forms.padding(2); this.dotbutton.name = "dotbutton"; this.dotbutton.size = new system.drawing.size(58, 41); this.dotbutton.tabindex = 10; this.dotbutton.text = "."; this.dotbutton.usevisualstylebackcolor = true; this.dotbutton.click += new system.eventhandler(this.dotbutton_click); // // addbutton // this.addbutton.location = new system.drawing.point(135, 230); this.addbutton.margin = new system.windows.forms.padding(2); this.addbutton.name = "addbutton"; this.addbutton.size = new system.drawing.size(38, 41); this.addbutton.tabindex = 11; this.addbutton.text = "+"; this.addbutton.usevisualstylebackcolor = true; this.addbutton.click += new system.eventhandler(this.operatorbutton_click); // // subtractbutton // this.subtractbutton.location = new system.drawing.point(135, 184); this.subtractbutton.margin = new system.windows.forms.padding(2); this.subtractbutton.name = "subtractbutton"; this.subtractbutton.size = new system.drawing.size(38, 41); this.subtractbutton.tabindex = 12; this.subtractbutton.text = "-"; this.subtractbutton.usevisualstylebackcolor = true; this.subtractbutton.click += new system.eventhandler(this.operatorbutton_click); // // multiplybutton // this.multiplybutton.location = new system.drawing.point(135, 139); this.multiplybutton.margin = new system.windows.forms.padding(2); this.multiplybutton.name = "multiplybutton"; this.multiplybutton.size = new system.drawing.size(38, 41); this.multiplybutton.tabindex = 13; this.multiplybutton.text = "*"; this.multiplybutton.usevisualstylebackcolor = true; this.multiplybutton.click += new system.eventhandler(this.operatorbutton_click); // // dividebutton // this.dividebutton.location = new system.drawing.point(135, 93); this.dividebutton.margin = new system.windows.forms.padding(2); this.dividebutton.name = "dividebutton"; this.dividebutton.size = new system.drawing.size(38, 41); this.dividebutton.tabindex = 14; this.dividebutton.text = "/"; this.dividebutton.usevisualstylebackcolor = true; this.dividebutton.click += new system.eventhandler(this.operatorbutton_click); // // clearbutton // this.clearbutton.location = new system.drawing.point(9, 48); this.clearbutton.margin = new system.windows.forms.padding(2); this.clearbutton.name = "clearbutton"; this.clearbutton.size = new system.drawing.size(80, 41); this.clearbutton.tabindex = 15; this.clearbutton.text = "clear"; this.clearbutton.usevisualstylebackcolor = true; this.clearbutton.click += new system.eventhandler(this.clearbutton_click); // // equalsbutton // this.equalsbutton.location = new system.drawing.point(93, 48); this.equalsbutton.margin = new system.windows.forms.padding(2); this.equalsbutton.name = "equalsbutton"; this.equalsbutton.size = new system.drawing.size(80, 41); this.equalsbutton.tabindex = 16; this.equalsbutton.text = "="; this.equalsbutton.usevisualstylebackcolor = true; this.equalsbutton.click += new system.eventhandler(this.equalsbutton_click); // // outputtextbox // this.outputtextbox.location = new system.drawing.point(9, 3); this.outputtextbox.margin = new system.windows.forms.padding(2); this.outputtextbox.multiline = true; this.outputtextbox.name = "outputtextbox"; this.outputtextbox.size = new system.drawing.size(164, 41); this.outputtextbox.tabindex = 17; // // form1 // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(184, 280); this.controls.add(this.outputtextbox); this.controls.add(this.equalsbutton); this.controls.add(this.clearbutton); this.controls.add(this.dividebutton); this.controls.add(this.multiplybutton); this.controls.add(this.subtractbutton); this.controls.add(this.addbutton); this.controls.add(this.dotbutton); this.controls.add(this.ninebutton); this.controls.add(this.eightbutton); this.controls.add(this.sevenbutton); this.controls.add(this.sixbutton); this.controls.add(this.fivebutton); this.controls.add(this.fourbutton); this.controls.add(this.threebutton); this.controls.add(this.twobutton); this.controls.add(this.onebutton); this.controls.add(this.zerobutton); this.margin = new system.windows.forms.padding(2); this.name = "form1"; this.text = "form1"; this.resumelayout(false); this.performlayout(); } #endregion private system.windows.forms.button zerobutton; private system.windows.forms.button onebutton; private system.windows.forms.button twobutton; private system.windows.forms.button threebutton; private system.windows.forms.button fourbutton; private system.windows.forms.button fivebutton; private system.windows.forms.button sixbutton; private system.windows.forms.button sevenbutton; private system.windows.forms.button eightbutton; private system.windows.forms.button ninebutton; private system.windows.forms.button dotbutton; private system.windows.forms.button addbutton; private system.windows.forms.button subtractbutton; private system.windows.forms.button multiplybutton; private system.windows.forms.button dividebutton; private system.windows.forms.button clearbutton; private system.windows.forms.button equalsbutton; private system.windows.forms.textbox outputtextbox; } }
i suspect want
outputtextbox.text += dotbutton.text; (you missed +)
Comments
Post a Comment