10 Replies - 467 Views - Last Post: 28 July 2013 - 12:36 PM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Error with calculator program

Posted 25 July 2013 - 06:27 AM

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtDisplay_TextChanged(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + txtDisplay.Text;
    }
    protected void Button1(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Buttonn1;
    }
    protected void Button2(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button22;
    }
    protected void Button3(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button33;
    }
    protected void Button4(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button44;
    }
    protected void Button5(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button55;
    }
    protected void Button6(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button66;
    }
    protected void Button7(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button77;
    }
    protected void Button8(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button88;
    }
    protected void Button9(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button99;
    }
    protected void Button10(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button100;
    }
    protected void Button11(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button111;
    }
    protected void Button12(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button121;
    }
    protected void Button13(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button131;
    }
    protected void Button14(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button141;
    }
    protected void Button15(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button151;
    }
    protected void Button16(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button161;
    }
    protected void Button17(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button141;
    }
    protected void Button18(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button181;
    }
    protected void Button19(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button191;
    }
    protected void Button20(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button201;
    }
}



When I try and use this calculator I get: System.Web.UI.WebControls; , In the text box instead of the number ive selected.
What am I doing wrong here because I cant figure it out.

Is This A Good Question/Topic? 0
  • +

Replies To: Error with calculator program

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Error with calculator program

Posted 25 July 2013 - 06:31 AM

Beside the repeating code, i can't see what the error is?
Could you please write it here.
Was This Post Helpful? 0
  • +
  • -

#3 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Error with calculator program

Posted 25 July 2013 - 06:41 AM

There is no error message as its runs fine but when you click on any of the buttons on the calculator the only thing that comes up in the text box, where the numbers are supposed to show, is the this:
System.Web.UI.WebControls.Button
Was This Post Helpful? 0
  • +
  • -

#4 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Error with calculator program

Posted 25 July 2013 - 06:52 AM

View PostBrendanH, on 25 July 2013 - 06:27 AM, said:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtDisplay_TextChanged(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + txtDisplay.Text;
    }
    protected void Button1(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Buttonn1;
    }
    protected void Button2(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button22;
    }
    protected void Button3(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button33;
    }
    protected void Button4(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button44;
    }
    protected void Button5(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button55;
    }
    protected void Button6(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button66;
    }
    protected void Button7(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button77;
    }
    protected void Button8(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button88;
    }
    protected void Button9(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button99;
    }
    protected void Button10(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button100;
    }
    protected void Button11(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button111;
    }
    protected void Button12(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button121;
    }
    protected void Button13(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button131;
    }
    protected void Button14(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button141;
    }
    protected void Button15(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button151;
    }
    protected void Button16(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button161;
    }
    protected void Button17(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button141;
    }
    protected void Button18(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button181;
    }
    protected void Button19(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button191;
    }
    protected void Button20(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + Button201;
    }
}



When I try and use this calculator I get: System.Web.UI.WebControls; , In the text box instead of the number ive selected.
What am I doing wrong here because I cant figure it out.


Where is Text property for button's???

Do like this: txtDisplay.Text = txtDisplay.Text + Button201.Text;

Add .Text to all buttons.
Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Error with calculator program

Posted 25 July 2013 - 07:25 AM

What are those Button1, Button33, Button44 variables?
Was This Post Helpful? 0
  • +
  • -

#6 koeshkoesh  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 27-December 08

Re: Error with calculator program

Posted 25 July 2013 - 10:08 AM

txtDisplay.Text = txtDisplay.Text + Button44;

When you are saying txtDisplay.Text + Button44 you want to add the button to the text, which is impossible since buttons ain't text.
You want to add the text of the button to the txtDisplay.text? You should do this by adding Button44.Text to the txtDisplay.
in this case your code would become:
txtDisplay.Text = txtDisplay.Text + Button44.Text;

Keep in mind that you cannot add controls to a text. You need to get the value which the control is holding by gettin the value from a property of the button such as Button44.Text;
This is why it says System.Web.UI.WebControls right now, since you're adding the Button control to the text.

This post has been edited by koeshkoesh: 25 July 2013 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#7 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Error with calculator program

Posted 26 July 2013 - 02:51 AM

protected void button20(object sender, EventArgs e)
{
   total1 = total 1 + double.Parse(txtDisplay.Text);
}




Im getting this error message but Im not sure what needs to be changed
{"Input string was not in a correct format."}
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: Error with calculator program

Posted 28 July 2013 - 09:02 AM

Well yeah... Because you are trying to add numbers and text.
What is the answer to:
5 + "Yogi Bear" =

Exactly! You can't add a number and a string. You have to convert the text to a number first.

Next... As others have mentioned, the lack of proper naming of your buttons is terrible. For buttons 0-9 I get it. btn1 makes sense. After that name them immediately after you make them! btnPlus, btnMinus {...}


And this repetitive code where you have a handler for every button is aweful. You don't need that. You can have just one handler for all the buttons. Look at the signature for the button handler. Notice that it receives the actual sending object as (object sender)... That's the button you clicked. So you can get the text off that.


protected void AnyButton_Clicked(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + ((Button)sender).Text;
    }

See what we did here?
  • Take the sender...
  • Cast it to a Button
  • Get its .Text
  • Then add it to the already displayed text

Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Error with calculator program

Posted 28 July 2013 - 09:59 AM

... and you use the AnyButton_Clicked method signature for every click event of your button control
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: Error with calculator program

Posted 28 July 2013 - 10:09 AM

Not sure what you mean by that Michael26. You would have all your buttons use this one method for the .Click event handler. All 45 buttons (or whatever) would point to this one method. They wouldn't all each have their own handler that uses this same signature, which kind of sounds like what you are saying. One method (handler) for all buttons.

tlhIn`toq said:

You can have just one handler for all the buttons.

This post has been edited by tlhIn`toq: 28 July 2013 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#11 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Error with calculator program

Posted 28 July 2013 - 12:36 PM

One button control has one click event, so for each button click event i would assign AnyButton_Clicked, kinda like
Button.Click += AnyButton_Clicked()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1