[IMG]http://i44.tinypic.com/jszh5h.jpg[/IMG]
So I played around in C# yesterday, and decided to do something simple. I thought what better way to use math than put more math in, so I made a simple calculator. It's very straightforward, not much to it besides 4 buttons.
*KNOWN BUGS*
-Putting letters in the text boxes crashes the app (I know, who would do that? Well since no one likes crashes, I'll fix this)
-Leaving a text box empty then pressing one of the buttons crashes the app (Thanks raccoon12)
[url=http://solidfiles.com/d/3N5W][img]http://solidfiles.com/info_imgs/3N5W.jpg[/img][/url]
It's my first applet, so I recommend you check it out. :D
-Beau
[highlight]Source code down there v.v[/highlight]
Also post the source code, please.
Crashed when I clicked times.
Hmm, I'll have to look into that...
[editline]10:47AM[/editline]
Erectin' a calculator...
[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 SimpCalc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
double number1;
double number2;
double result;
number1 = double.Parse(textBox1.Text);
number2 = double.Parse(textBox2.Text);
result = number1 + number2;
textBox3.Text = result.ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
double number1;
double number2;
double result;
number1 = double.Parse(textBox1.Text);
number2 = double.Parse(textBox2.Text);
result = number2 - number1;
textBox3.Text = result.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
double number1;
double number2;
double result;
number1 = double.Parse(textBox1.Text);
number2 = double.Parse(textBox2.Text);
result = number1 * number2;
textBox3.Text = result.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
double number1;
double number2;
double result;
number1 = double.Parse(textBox1.Text);
number2 = double.Parse(textBox2.Text);
result = number2 / number1;
textBox3.Text = result.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}
[/code]
I'm new to C#, so please, help me improve.
When you don't enter a second number it crashes.
Try this: [url]http://fatagnus.com/why-you-should-use-tryparse-in-c/[/url]
And check that number1 is != 0 for division ;)
Ok, time to help me out...
-Not entering a number or completing the forms crashes
-Typing letters into it crashes it.
-Pressing a button before completing a form crashes it.
Solution? Have "Please Enter a Number" or "Please Enter numbers in both forms" appear instead of an answer.
Any help on how I can do this?
You need some error trapping. I don't know C#, but you want the program to check for a number in both text boxes [B]before[/B] it does the sum. It also needs to make sure there is no 0 in the second text box for a division.
[QUOTE=Beau Phos;16258805]
-Not entering a number or completing the forms crashes
[/quote]
Check if [url=http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx]string.IsNullOrEmpty[/url] is true on the text values, if it is: don't do any calculations.
[QUOTE=Beau Phos;16258805]
-Typing letters into it crashes it.
[/quote]
When parsing the string values into integers, either try/catch the area for a [url=http://msdn.microsoft.com/en-us/library/system.formatexception.aspx]FormatException[/url], or make use of [url=http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx]int.TryParse[/url]/[url=http://msdn.microsoft.com/en-us/library/system.single.tryparse.aspx]float.TryParse[/url] to see if the value correctly parses.
[QUOTE=Beau Phos;16258805]
-Pressing a button before completing a form crashes it.
[/quote]
The above two should help with this.
Use TryParse, it's much better.
What happened to bold tags?
[QUOTE=nos217;16290918]What happened to bold tags?[/QUOTE]
[b]Nothing?[/b]
Maybe set the default values to 1?
[QUOTE=Nick99;16339181]Maybe set the default values to 1?[/QUOTE]
that's like putting a bandaid over a 3 inch thick laceration
Sorry, you need to Log In to post a reply to this thread.