在c# visual studio中,我希望我的程序在重量和批发价格中验证整数,因此人们不能键入十进制数



需要对权重和批发价格进行验证,小数在顶部声明。这是一个车辆登记费用计算器,计算车辆税、印花税、保险费和总登记费用

        //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();          

使用正则表达式验证键入、上键或预览输入事件

<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; 
  }
}
上面的

将自动处理任何非整数的输入。阻止输入甚至显示在文本框中还可以看看这些类似的问题:仅用于数字的正则表达式和只接受数字(0-9)和不接受字符的正则表达式[duplicate]

您可以要求文本框输入验证其Validating事件处理程序:

    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;
    }

这样你的"calculation"方法就不必为输入正确的文本框值而烦恼了:

        //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();

最新更新