Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,549 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,868 people online right now. Registration is fast and FREE... Join Now!




C# Calculator Trouble

 
Reply to this topicStart new topic

C# Calculator Trouble, Decimal Button Trouble

CruorAvis
15 Aug, 2008 - 08:52 PM
Post #1

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 17

Ello',

I'm teaching myself C# in a bit of a rush for an internship position at a software company - The tutorial I'm taking now is actually pretty good but some of the exercises are rather intuitive and this has been driving me insane for an hour.

I'm using Visual C# Express 2008 and attempting to build a Calculator so far everything is working but i cannot get the Decimal button to work (btnPoint).

Any help would be greatly appreciated - Sorry for such a stupid question.

In the underlying code i'm receiving an SystemFormat.Exception error during run time.

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        double total1 = 0;
        double total2 = 0;
        private void button4_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn4.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text+btn1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn2.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn3.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn5.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn6.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn7.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn8.Text;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn9.Text;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btn0.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
        }

        
        private void btnPlus_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();

        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            total2 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Text = total2.ToString();
            total1 = 0;
        }

        private void btnPeriod_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + double.Parse(btnPeriod.Text);
        }
    }
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: C# Calculator Trouble
15 Aug, 2008 - 09:57 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Here is a tutorial on creating a Calculator in C# that may help answer a lot of the questions you have smile.gif
User is online!Profile CardPM
+Quote Post

eclipsed4utoo
RE: C# Calculator Trouble
16 Aug, 2008 - 06:41 AM
Post #3

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
as you can see in your code:

csharp

private void btnPeriod_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + double.Parse(btnPeriod.Text);
}


you are trying to convert the text of the button to a double. while this works fine when the button contains a number as it's Text, this will not work when the text is a ".". change your code to this..

csharp

private void btnPeriod_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPeriod.Text;
}

User is offlineProfile CardPM
+Quote Post

CruorAvis
RE: C# Calculator Trouble
16 Aug, 2008 - 08:46 AM
Post #4

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 17

Thanks, it seems so obvious now!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:07PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month