如何做一个计算用户输入的总米数的草坪程序 [英] How to do a lawn program that calculate the total metres entered by the user

查看:17
本文介绍了如何做一个计算用户输入的总米数的草坪程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个计算草坪大小的程序 - 它将有 2 个文本框和一个标签length * width = total meter.我的问题是,当我输入一个十进制值时,它显示一个错误,但是当我在文本框中输入整数值时,它没有显示错误并计算它,我做的一切都是正确的,我的代码看起来像这样

I'm trying to do a program that will calculate the size of lawn - it will have 2 textboxes and a label length * width = total metres. My problem is that when I put in a decimal value it shows an error but when I put integer value on the text box it shows no error and calculates it and I did everything right my code looks like this

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim decLegth, decWidth, decSize As Decimal
        decLegth = Convert.ToDecimal(txtLength.Text)
        decWidth = Convert.ToDecimal(txtWidth.Text)
        decSize = decLegth * decWidth
        lblDisplay.Text = decSize.ToString("N2")

我会遗漏什么?

推荐答案

这很可能是文化问题.您会看到,在某些国家/地区,小数点由 , 分隔,而其他国家/地区则使用 ..

It's most likely a culture problem. You see, in some countries decimals are separated by a , while others use the ..

这是我将应用程序的文化设置为挪威语的方式:

This is how I set the culture of my application to Norwegian:

System.Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("nb-NO")
System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("nb-NO")

所以现在,如果我尝试转换,我会这样做:

So now, if i try convert i do like this:

Dim d As Decimal = Convert.ToDecimal("10,0", System.Threading.Thread.CurrentThread.CurrentCulture)

这是一个文化代码列表 这样你就可以找到你的了.

Here's a list of culture codes so you can find yours.

另一方面,您应该使用 NumericUpDown 控件.它公开显示的文本"的十进制 Value 属性.

On the other hand, instead of TextBox you ought to use the NumericUpDown control. It exposes a decimal Value property of the displayed "text".

这篇关于如何做一个计算用户输入的总米数的草坪程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆