Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
    float a;
    float b;
    float c;
    bool plus = false;
    bool minus = false;
    bool multiply = false;
    bool divide = false;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "1";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "2";
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "3";
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "4";
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "5";
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "6";
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "7";
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "8";
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "9";
    }
    protected void Button10_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + "0";
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + ".";
    }
    protected void Button12_Click(object sender, EventArgs e)
    {
       
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        plus = true;
    }
    protected void Button13_Click(object sender, EventArgs e)
    {
    
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        minus = true;
    }
    protected void Button14_Click(object sender, EventArgs e)
    {
        
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        multiply = true;
    }
    protected void Button15_Click(object sender, EventArgs e)
    {
       
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        divide = true;
    }
    protected void Button16_Click(object sender, EventArgs e)
    {
        b = Convert.ToInt32(TextBox1.Text);
        if (plus == true)
        {
            c = a + b;
            TextBox1.Text = Convert.ToString(c);
        }
        else if (minus == true)
        {
            c = a - b;
            TextBox1.Text = Convert.ToString(c);
        }
        else if (multiply == true)
        {
            c = a * b;
            TextBox1.Text = Convert.ToString(c);
        }
        else if (divide == true)
        {
            c = a / b;
            TextBox1.Text = Convert.ToString(c);
        }
        else
        {
            TextBox1.Text = "";
        }
    }
    protected void Button17_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        a = 0;
        b = 0;
        c = 0;
        plus = false;
        minus = false;
        multiply = false;
        divide = false;
    }
}



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 28-May-16 18:25pm
v3
Comments
Syed Salman Raza Zaidi 7-Jul-11 4:01am    
What problem is arising? tell in detail
walterhevedeich 7-Jul-11 4:04am    
Try to check your ASP.Net control tags if the events were specified.
darealkleyr 7-Jul-11 6:08am    
Upon clicking button16, it should display in textbox1 whatever the computations are, but it so happen that it didn't display anything. Kindly please help me. Thank you. :)
darealkleyr 12-Jul-11 19:55pm    
I already figure out what is the problem with this code, It is so simple you just have to declare the global variables with the public static in the beginning of it. (e.g. public static float a;). You guys talk to much about your expertise blah blah blah but you're no help to anyone. You are just boasting yourself off. Is this how you respond to anyone who is in need of your help? I regret posting my code and asking for help in this forum. Anyways, thank you for pissing me off. :)

In my opinion, this is beyond help, because this code shows that you did not grasp an idea on what programming is. Programming is basically all about abstractions and not repeating any piece of code.

Repeating "TextBox1.Text = TextBox1.Text + " and other fragments several times shows that you don't know how to approach programming in general — just yet. Other problems include using immediate constants hard-coded in the code especially string ones. Even "" is not acceptable (use string.Empty).

What else? Names like Buttton16_Click break all thinkable naming conventions. You should never use auto-generated names, always rename them into semantically sensible names. Visual Studio re-factoring engine makes it really easy. The Boolean variables plus, minus, multiply and divide make no sense. Empty method Page_Load should not present.

What do we have? There is practically no a single correct line. What would I advice? Abandon this project and make a big step back. Do most simple exercises to learn how value and reference types, fields, properties, events, classes, structures and instances, parameters and variable work. Look at good but simple code samples and understand them. Take some detailed manual of language and .NET and read it all, without skipping anything with exercises.

See also: I have a problem with my program. Please help![^].

—SA
 
Share this answer
 
v2
Comments
OriginalGriff 7-Jul-11 4:06am    
Harsh, but fair - it has to be said, sometimes.
Sergey Alexandrovich Kryukov 7-Jul-11 4:12am    
Thank you, Griff. I appreciate your response and agree.
There is nothing negative in my recommendations -- I really think it's the best for OP to accept them; otherwise much bigger frustrations are unavoidable.
--SA
fjdiewornncalwe 7-Jul-11 16:26pm    
Agreed
Sergey Alexandrovich Kryukov 7-Jul-11 16:50pm    
Thank you, Marcus.
--SA
Christoph Keller 8-Jul-11 3:25am    
Totally agree, my 5!
"What might be the problem"

Do you want a list?

You can't just lift code from WinForms and expect it to work in a completely different environment: specifically a client/server environment.
You do realize that with web programs, the program starts from scratch each time the user posts to the server? That the server isn't sitting there, waiting in case the user might press a button? That your local variables are initialized each time?

Even if this approach would work (which it could be twisted and hammered into doing) it would still be a very stupid idea. Every time the user hit a key, it posts the to the server, which works out what is going on, and sends back a new page, updated to reflect that. Slow, ugly and clumsy does not even begin to describe such a calculator.

Learn what is going on, learn some javascript, and try again from scratch. (It's not even a good way to do a calculator in Winforms!)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jul-11 4:13am    
Good points, certainly. My 5.
--SA
Sergey Alexandrovich Kryukov 7-Jul-11 4:19am    
By the way, speaking of the calculators... After reading your assessment of the calculator design (which I agree, with of course) I remembered my own Javascript calculator.

I posted a link in my other solution here, please take a look.
--SA
OriginalGriff 7-Jul-11 4:27am    
I hadn't forgotten it! :laugh:
I didn't post the link because I thought the OP might decide to copy it...Not that they would cheat on their homework, I'm sure!
darealkleyr 7-Jul-11 5:54am    
If I will just copy another one's work, I shouldn't have post my code here. I will just copy their work. Anyways, thank you for your response. :)
Christoph Keller 8-Jul-11 3:27am    
Exactly. Poor webserver hosting such a calc app... ;) my 5!
Speaking of the calculator: I agree with Griff.
I would make one step further: who needs a calculator with buttons? Such calculator imitates some stone-age pocket calculators, but why? They have been designed this way only due to the small size, weak processor, etc.

So, here is how a calculator should work: http://www.sakryukov.org/freeware/calculator/[^].

This is my Javascript calculator, all in a single HTML file.

—SA
 
Share this answer
 
Comments
darealkleyr 7-Jul-11 5:49am    
You can go tell my professor about having us make a calculator with buttons. :)
Sergey Alexandrovich Kryukov 11-Jul-11 0:46am    
I would do it with pleasure; one way to do it is giving him a link to this page :-).
--SA
darealkleyr 12-Jul-11 19:53pm    
If you want to see him, you make your own move, don't ask anything from me. You know what, I already figure out what is the problem with this code, It is so simple you just have to declare the global variables with the public static in the beginning of it. (e.g. public static float a;). You guys talk to much about your expertise blah blah blah but you're no help to anyone. You are just boasting yourself off. Is this how you respond to anyone who is in need of your help? I regret posting my code and asking for help in this forum. Anyways, thank you for pissing me off. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900