Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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 Dental : Form
    {
        public Dental()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int total;
            int cleaning, cavity, xray, fluoride, rootcanal, other;
            cleaning = 35;
            cavity = 150;
            xray = 85;
            fluoride = 50;
            rootcanal = 225;

            total = 0;
            other = Int32.Parse(TxtOther.Text);


            if (((ChkCleaning.Checked = false) &&
           (ChkCavity.Checked = false) && (TxtName.Text == String.Empty) &&
            (ChkXray.Checked = false) && (TxtOther.Text == String.Empty) &&
            (ChkFluoride.Checked = false) &&
            (ChkRootCanal.Checked = false) &&
            (ChkOther.Checked = false)))
            {
                MessageBox.Show("Please check at least one checkbox!, and enter your name", "Incorrect Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }









            else if ((ChkOther.Checked==true) && (string.IsNullOrEmpty(TxtOther.Text)))
            {
                MessageBox.Show("Please enter an amount for other!", "Incorrect Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }

            else if (ChkOther.Checked == false)
            {
                TxtOther.Text = "";
            }


























            else


            {
            if (ChkCleaning.Checked == true)
            {
                total += cleaning;
                LblTotal.Text = String.Format("{0:C}", total);
            }

            if (ChkCavity.Checked == true)
            {
                total += cavity;
                LblTotal.Text = String.Format("{0:C}", total);
            }

            if (ChkXray.Checked == true)
            {
                total += xray;
                LblTotal.Text = String.Format("{0:C}", total);
            }


           if (ChkFluoride.Checked == true)
            {
                total += fluoride;
                LblTotal.Text = String.Format("{0:C}", total);
            }


            if (ChkRootCanal.Checked == true)
            {
                total += rootcanal;
                LblTotal.Text = String.Format("{0:C}", total);
            }


            if (ChkOther.Checked == true)
            {
                total += other;
                LblTotal.Text = String.Format("{0:C}", total);

            }
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            ChkCleaning.Checked = false;
            ChkCavity.Checked = false;
            ChkXray.Checked = false;
            ChkFluoride.Checked = false;
            ChkRootCanal.Checked = false;
            ChkOther.Checked = false;
            TxtName.Text = "";
            TxtOther.Text = "";


        }
    }
}

C#

Posted
Comments
Member 11190214 3-Nov-14 10:31am    
When I run this program I get the error "Input string was not in a correct format"
Afzaal Ahmad Zeeshan 3-Nov-14 10:44am    
You can add this piece of information to the question itself.

Quote:
When I run this program I get the error "Input string was not in a correct format"

btw, you description of problem and the question text are not inline at all.

The first possible line of code where I can see this error occurrence is:

other = Int32.Parse(TxtOther.Text);


You can start using TryParse[^]. It's always good to have a defensive code.

C#
other = Int32.Parse(TxtOther.Text);

int number;

bool result = Int32.TryParse(TxtOther.Text, out other);

if (!result)
{
    //TxtOther.Text is not a valid number
}
 
Share this answer
 
I invite you to think about a UI strategy where you only enable Controls when their being enabled "means something." So, you start a entry-session with certain Controls disabled, like the 'TotalAndCreateBill Button, and the TextBox associated with 'Other.

Although this may be a bit extreme: you could even disable all the Controls except the TextBox for 'Name until the user entered some valid Text.

Example: No services' CheckBoxes are Checked: the Button that when clicked calculates a total and creates a bill stays disabled.

Example: 'Other CheckBox is not Checked: then the TextBox for entering Text for the cost of 'Other is either disabled, or hidden.

Example: 'Other CheckBox is Checked, but there's nothing entered in its (now visible, enabled) associated TextBox: the Button that when clicked calculates a total and creates a bill stays disabled.

Another strategy is to validate the content of any user input as soon as possible: the moment the user switches focus from the 'Name, or 'Other TextBoxes, you can have the 'Leave event of those Controls check the input, and take action if the input is not valid. If the input is invalid, you can present an error message, clear the Textbox, and set the 'Focus property of the TextBox to 'true to redirect the user's attention to it:
C#
private float otherValue;

private void tbOther_MouseLeave(object sender, EventArgs e)
{
    if (! Single.TryParse(textBox_surname.Text, out otherValue))
    {
        otherValue = 0;
        tbOther.Clear();
        MessageBox.Show("Please enter a valid number for the cost of the other service.");
        tbOther.Focus();
    }
}
 
Share this answer
 

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