Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hr As Integer
Dim pf As Integer
Dim da As Integer
Dim ta As Integer
Dim net As Integer
hr = Val((TextBox1.Text * 5.5) / 100)
pf = Val((TextBox1.Text * 10.5) / 100)
da = Val((TextBox1.Text * 6) / 100)
ta = Val((TextBox1.Text * 3.5) / 100)
TextBox2.Visible = True
TextBox3.Visible = True
TextBox4.Visible = True
TextBox5.Visible = True
TextBox6.Visible = True
TextBox2.Text = hr
TextBox3.Text = pf
TextBox4.Text = da
TextBox5.Text = ta
net = Val(TextBox1.Text + TextBox2.Text + TextBox4.Text) - Val(TextBox3.Text + TextBox5.Text)
TextBox6.Text = CLng(net)
End Sub
问问题
1412 次
3 回答
1
虽然@Nadeem_MK 的答案是正确的,但您的解决方案还有其他问题:
- 您使用双倍的员工收入。我认为小数会是一个更好的选择,因为您正在处理财务数据,并且众所周知,双精度值在小值时不精确。
- 您只是将字符串(文本)和双打放在一起 - 这是难以找到错误的秘诀。帮自己一个忙并激活 Option Strict on - 您还有更多工作要做,但代码中的错误会少得多。信息: http: //support.microsoft.com/kb/311329/de
- 像“TextBox1.Text * 5.5”这样的语句存在风险 - 如果用户输入“A”,代码会立即崩溃,因为“A”不能转换为双精度。使用类似 Double 的东西。TryParse 仅接受有效数字并在不是数字时显示错误消息。
于 2013-09-26T12:20:16.493 回答
0
只需将局部变量的类型更改为double;
Dim hr As Double
Dim pf As Double
Dim da As Double
Dim ta As Double
Dim net As Double
它通常应该可以正常工作。
于 2013-09-26T12:14:31.177 回答
0
现在是 2013 年,无论选项是否严格,这就是您的代码应该是什么样子。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hr As Double
Dim pf As Double
Dim da As Double
Dim ta As Double
Dim net As Double
hr = (CDbl(TextBox1.Text) * 5.5) / 100
pf = (CDbl(TextBox1.Text) * 10.5) / 100
da = (CDbl(TextBox1.Text) * 6) / 100
ta = (CDbl(TextBox1.Text) * 3.5) / 100
TextBox2.Visible = True
TextBox3.Visible = True
TextBox4.Visible = True
TextBox5.Visible = True
TextBox6.Visible = True
TextBox2.Text = CStr(hr)
TextBox3.Text = CStr(pf)
TextBox4.Text = CStr(da)
TextBox5.Text = CStr(ta)
Dim net As Double = (CDbl(TextBox1.Text) + hr + da) - (pf + ta)
TextBox6.Text = CStr(net)
End Sub
于 2013-09-26T12:21:48.197 回答