Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello!
I'm doing a C# tutorial (homeandlearn.co.uk) and i'm currently making a calculator.
However, I've gotten to the point where I'll make the calculator accept "dots" and I dont know what "symbol" to use for just dot... Alright, here's the 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();
        }
        double total1 = 0;
        double total2 = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button1.Text;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

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

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button9.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button6.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button5.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button8.Text;
        }

        private void button0_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button0.Text;
        }

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

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

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

        private void btnPoint_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + ".";
        }
    }
}


Please do note that the btn point is at the end.

Again sorry for the noobish questions at a such well established place as this.
[b]At its current state, the program crashes whenever i press +, or =, [/b]


The lesson said this: "You have not yet written any code for the btnPoint button. This means that you can't have numbers like 10.5 or 36.7 in your additions. Write code to solve this. (Hint: you only need one line of code.)"





error i get:
MSIL
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at WindowsFormsApplication1.Form1.btnPlus_Click(Object sender, EventArgs e) in C:\Users\Weidrup\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 82
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.225 (RTMGDR.030319-2200)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WindowsFormsApplication1
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Weidrup/AppData/Local/Temporary%20Projects/WindowsFormsApplication1/bin/Release/WindowsFormsApplication1.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Posted
Updated 7-May-11 14:12pm
v4
Comments
Fabio V Silva 7-May-11 20:00pm    
What's the error you get?
Member 7896310 7-May-11 20:27pm    
Updated the code with error message.
Fabio V Silva 7-May-11 20:30pm    
See my solution below.
Member 7896310 7-May-11 20:12pm    
It crashes... I suppose the btnPlus dont like the "+".
Sergey Alexandrovich Kryukov 8-May-11 0:42am    
What a huge anti-advertisement for homeandlearn.co.uk!
--SA

It is a common beginner technique to create a button handler for each button click event; however it is absolutely unnecessary.

protected void OnButtonClick(object sender, EventArgs e)
{
  textBox1.Text += ((Button)sender).Tag;
}


Assign the proper number or symbol (period is correct for decimal point) to the Tag property for the button and you will have much cleaner and smaller code.
 
Share this answer
 
Comments
Member 7896310 7-May-11 20:02pm    
Well, I really understand what you meant, and appreciate you trying, but however I really wanna solve this in the most beginner way (as i've done sofar)
[no name] 7-May-11 20:08pm    
You mean by stumbling around and not learning how to do it correctly or professionally? By all means, continue, just don't ask help here then
Member 7896310 7-May-11 20:19pm    
Mark, dont be so pissy! Please
Sergey Alexandrovich Kryukov 8-May-11 0:45am    
There is nothing "pissy" here. Just the opposite -- this is a decent answer. Follow the advise, it's good.
--SA
Member 7896310 7-May-11 20:03pm    
Also, textBox1.Text = textBox1.Text + peroid;
Doesn't work (really a beginner here).
You're system is probably configured to use a "," as a decimal separator and not a ".".

Try using a comma and see if it works.
 
Share this answer
 
Comments
Member 7896310 7-May-11 20:31pm    
You, sir, just saved my day. Seing as I'm Swedish, and the code is american, things got a bit messed up! But thank you so much! =)
Fabio V Silva 7-May-11 20:37pm    
You're welcome. Do some more research on globalization, you'll need it to prevent these kind of problems. This was just a quick solution to your specific problem.
Wonde Tadesse 7-May-11 20:40pm    
Sorry my friend. Even after "," you should validate the input. Agreed ?
Fabio V Silva 7-May-11 20:45pm    
He is not inputting anything per se, he's just pressing a button that adds a comma to a textbox, what do you want to validate?
And he is just following some tutorial, if he is really interested in programming he'll get to the tutorials where he'll learn how to sanitize the inputs...
Member 7896310 7-May-11 20:54pm    
Also, this yes, i'm at the extreme basics, continuing on =)
Well, You should validate each user input with decimal value. This can be achieved with .NET Regular Expression, RegEx class.So for each number button validdate the input value, before performing the addition operation.

Here is how you will validate the value.
C#
if (!Regex.IsMatch(textBox1.Text, "^(-)?\\d*(\\.\\d+)?$"))// Decimal validator expression.
            {
                MessageBox.Show("Please put valid number");
                return;
            }
 
Share this answer
 
v2
Comments
Member 7896310 7-May-11 20:07pm    
Yes... Thing is... i WANT to use a decimal... not warn if there is one..
Sorry for being unclear.
Member 7896310 7-May-11 20:08pm    
Also, will use that one from now on whenever i wanna check something=) cheers
Wonde Tadesse 7-May-11 20:28pm    
Ok, What else do you want. This is the problem of incompleted input. So before performing operation do something. If you don't like the MessageBox, then display the message in Label Control or a ReadOnly mode TextBox.
[no name] 7-May-11 20:39pm    
Validating the textbox is unnecessary since the OP is not accepting input from it, only outputting to it. That is clear from the code sample posted
Wonde Tadesse 7-May-11 20:49pm    
Agreed.

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



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