-2

The validation needs to occur for the weight and the wholesalePrice, the decimals are declared at the top. This is a vehicle registration cost calculator that calculates vehicleTax, stampDuty, insurance premium, and total registration cost

        //get the user information that we need
        int weight = int.Parse(txtWeight.Text);
        decimal wholesalePrice = decimal.Parse(txtwholesalePrice.Text);
        decimal vehicleTax = 0.00m;
        decimal stampDuty = 0.00m;
        decimal insurancepremium = 0.00m;
        decimal Payable = 0.00m;
        decimal registration = 0.00m;
        //Calculations for private registration                   
        if (radioPrivate.Checked == true)
        {
            if (wholesalePrice >= 0)
                stampDuty = wholesalePrice / 100m;                
        }
        {
            if (wholesalePrice >= 0)
                insurancepremium = wholesalePrice / 50m;
        }
        {                
            if (weight <= 0)
                vehicleTax = 0.00m;
            else if (weight <= 975.00)
                vehicleTax = 191.00m;
            else if (weight <= 1154.00)
                vehicleTax = 220.00m;
            else if (weight <= 1504.00)
            {
                vehicleTax = 270.00m;
            }
            else if (weight >= 1505.00)
                vehicleTax = 411.00m;
            Payable = stampDuty + regoFee + vehicleTax + insurancepremium; // calculations for total amount payable   
            registration = stampDuty + regoFee + vehicleTax + insurancepremium; // calculations for total registration
            // message for when input value does not equal designed values
            {
                if (weight <= 0)                
                    MessageBox.Show("Your weight value must be atleast above 0, please click the reset button and try again", "Input Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);                    
                if (wholesalePrice <= 0)
                    MessageBox.Show("Your Wholesale value must be atleast above 0, please click the reset button and try again", "Input Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);                   
            }                          
            // print the information on the screen
            txtVehicleTax.Text = vehicleTax.ToString();
            txtStampDuty.Text = stampDuty.ToString();
            txtinsurancePremium.Text = insurancepremium.ToString();
            txtpayable.Text = Payable.ToString();
            txtregistration.Text = registration.ToString();          
DrDusk
  • 1
  • 1

2 Answers2

0

Use regular expressions to validate your input either on keypress,keyup or preview input events

<TextBox Name="weight "      
Width="300" PreviewTextInput="NumberValidation"
Height="35"
HorizontalAlignment="Left"
FontSize="16" />

 //code behind
private  void NumberValidation(object sender, TextCompositionEventArgs e)
{
 var regex = new Regex("[^0-9]+");
 e.Handled = regex.IsMatch(e.Text);
}
//you could have this in your win forms app
private Regex NumberValidation()
{
  return new Regex(@"[^0-9]+");
}

private void weight_KeyPress(object sender, KeyPressEventArgs e)
{
  if (!NumberValidation().IsMatch(weight.Text))
  { 
     e.Handled = true; 
  }
}

the above will automatically handle any input which is not an integer.preventing the input from even showing in the text box also take a look at these similar questions: Regex for numbers only and Regex that accepts only numbers (0-9) and NO characters [duplicate]

Community
  • 1
  • 1
wsduho
  • 57
  • 7
  • Of course Regex is happy with this 'integer': "2147483648" and why do you assume that this is a WPF app? – Steve Oct 30 '16 at 08:28
  • @Steve i just provided a way for him to consume the regex function. similar could be done in winform as well – wsduho Oct 30 '16 at 08:39
0

you could demand textboxes input validation to their Validating event handler:

    int weight;
    private void txtWeight_Validating(object sender, CancelEventArgs e)
    {
        if (int.TryParse(txtWeight.Text, out weight))
        {
            if (weight > 0)
                return;                
        }
        txtWeight.Clear();
        e.Cancel = true;
    }

    decimal wholesalePrice;
    private void txtwholesalePrice_Validating(object sender, CancelEventArgs e)
    {
        if (decimal.TryParse(txtwholesalePrice.Text, out wholesalePrice))
        {
            if (wholesalePrice > 0)
                return;
        }
        txtwholesalePrice.Clear();
        e.Cancel = true;
    }

so that your "calculation" method must not bother about having the right typed textboxes input values:

        //get the user information that we need

        decimal vehicleTax = 0.00m;
        decimal stampDuty = 0.00m;
        decimal insurancepremium = 0.00m;
        decimal Payable = 0.00m;
        decimal registration = 0.00m;

        decimal regoFee = 0.00m;

        //Calculations for private registration                   
        if (radioPrivate.Checked)
        {
            if (wholesalePrice >= 0)
                stampDuty = wholesalePrice / 100m;
        }
        {
            if (wholesalePrice >= 0)
                insurancepremium = wholesalePrice / 50m;
        }



        if (weight <= 0)
            vehicleTax = 0.00m;
        else if (weight <= 975.00)
            vehicleTax = 191.00m;
        else if (weight <= 1154.00)
            vehicleTax = 220.00m;
        else if (weight <= 1504.00)
        {
            vehicleTax = 270.00m;
        }
        else if (weight >= 1505.00)
            vehicleTax = 411.00m;
        Payable = stampDuty + regoFee + vehicleTax + insurancepremium; // calculations for total amount payable   
        registration = stampDuty + regoFee + vehicleTax + insurancepremium; // calculations for total registration
                                                                            // message for when input value does not equal designed values
        {
            if (weight <= 0)
                MessageBox.Show("Your weight value must be atleast above 0, please click the reset button and try again", "Input Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (wholesalePrice <= 0)
                MessageBox.Show("Your Wholesale value must be atleast above 0, please click the reset button and try again", "Input Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        // print the information on the screen
        txtVehicleTax.Text = vehicleTax.ToString();
        txtStampDuty.Text = stampDuty.ToString();
        txtinsurancePremium.Text = insurancepremium.ToString();
        txtpayable.Text = Payable.ToString();
        txtregistration.Text = registration.ToString();
user3598756
  • 27,298
  • 4
  • 15
  • 25