I am designing a basic calculator using C#. I am facing difficulties while I am entering a decimal point. Example, if I am entering .8 then it gives me 0.8 which is correct if there is nothing on the display screen but after that if I am entering the decimal symbol, then also it accepts it that is 0.8..... I want only one decimal symbol to be accepted for one number. My code is
private void btn_Decimal_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol==true)
{
txt_Result.Text = txt_Result.Text + 0 + ".";
}
else
txt_Result.Text = txt_Result.Text + ".";
}
Here, if I am entering 0.9.999 then also it accepts and if I am entering 999..... then also it accepts. I want only one decimal symbol to be accepted for one number that is 999.999. Please help me. Also I am adding two additional labels that can show the current system date and time. I am unable to show the date as well as the time but I am able to show the date and time using VB.Net. I don't know where I am getting the errors. My whole code is
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 CS_Calculator
{
public partial class Form1 : Form
{
Boolean LastcharIssymbol {get;set;}
string op;
double a, memory;
public Form1()
{
InitializeComponent();
}
private void btn_1_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "1";
LastcharIssymbol= false;
}
private void btn_2_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "2";
LastcharIssymbol = false;
}
private void btn_3_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "3";
LastcharIssymbol = false;
}
private void btn_4_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "4";
LastcharIssymbol = false;
}
private void btn_5_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "5";
LastcharIssymbol = false;
}
private void btn_6_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "6";
LastcharIssymbol = false;
}
private void btn_7_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "7";
LastcharIssymbol = false;
}
private void btn_8_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "8";
LastcharIssymbol = false;
}
private void btn_9_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "9";
LastcharIssymbol = false;
}
private void btn_0_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "0";
LastcharIssymbol = false;
}
private void btn_Decimal_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol==true)
{
txt_Result.Text = txt_Result.Text + 0 + ".";
}
else
txt_Result.Text = txt_Result.Text + ".";
}
private void btn_Plus_Click(object sender, EventArgs e)
{
if(txt_Result.Text=="" || LastcharIssymbol)
{
MessageBox.Show("Please Enter first number to perform the addition operation.");
}
else
{
op = "+";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol=true;
}
}
private void btn_Minus_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to erform the substraction operation.");
}
else
{
op = "-";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Division_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to perform the division operation.");
}
else
{
op = "/";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Mult_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to perform the multiplication operation.");
}
else
{
op = "*";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Equal_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
txt_Result.Text = "";
}
private void btn_Clear_All_Click(object sender, EventArgs e)
{
txt_Result.Text = "";
op = "";
a = 0;
memory = 0;
}
private void btn_Memory_Click(object sender, EventArgs e)
{
memory = Convert.ToDouble(txt_Result.Text);
}
private void btn_Show_Memory_Click(object sender, EventArgs e)
{
txt_Result.Text = memory.ToString();
}
}
}
You should disable the decimal once it is clicked and enable it again if any of the operator or 'C' is pressed.
You could use decimal.TryParse to test whether the string is a valid decimal number. If not, such as if your input has two decimal points, then the TryParse call fails.
TryParse is a good option because there can be many issues with an entered number besides a doubled decimal point such as... a triple decimal point, a misplaced minus sign, alpha chars etc.
Try:
**Another example , 100% **
Change your last
else
to:Or consider disabling the decimal point button.
You should probably do some validation on the value to ensure it is a valid number.
Put this code in textbox_keypress here txtweight is my textbox name you use yours
private void txtweight_KeyPress(object sender, KeyPressEventArgs e) {
for the case you have text like '11.9+12' which has operations in it you can do the following