视觉基础 在我的代码中,我正在让用户输入四面墙中每一面的长度和高度。我得到了将长度乘以高度的代码,以计算出每个墙的面积,并在单独的文本框中显示四个区域中的每一个。现在,我想添加文本框中的四个数字,并在单击按钮时在单独的文本框中显示答案。
这是我目前的代码;
Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer
Area = TotalArea.Text
txtLength1 = Length1.Text
txtHeight1 = Height1.Text
txtLength2 = Length2.Text
txtHeight2 = Height2.Text
txtLength3 = Length3.Text
txtHeight3 = Height3.Text
txtLength4 = Length4.Text
txtHeight4 = Height4.Text
Area1.Text = txtLength1 * txtHeight1
Area2.Text = txtLength2 * txtHeight2
Area3.Text = txtLength3 * txtHeight3
Area4.Text = txtLength4 * txtHeight4
Area = Area1.Text + Area1.Text + Area3.Text + Area4.Text
End Sub
结束班
当我运行此代码时,我收到错误消息"转换为字符串""输入'整数'无效。"
答案 0 :(得分:0)
由于TextBox
控件将其内容存储为字符串/文本,因此您必须将这些文本值解析为数值,以便您可以将它们用于计算。
参见以下示例:
txtLength1 = int.Parse(Length1.Text)
修改强>
您还可以考虑使用int.TryParse
method代替int.Parse
来检测用户输入非数字文本的时间。
答案 1 :(得分:0)
没有考虑到人们倾向于写除整数之外的其他东西 这样可以解决问题:
Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer
Area = Convert.ToInt32(TotalArea.Text)
txtLength1 = Convert.ToInt32(Length1.Text)
txtHeight1 = Convert.ToInt32(Height1.Text)
txtLength2 = Convert.ToInt32(Length2.Text)
txtHeight2 = Convert.ToInt32(Height2.Text)
txtLength3 = Convert.ToInt32(Length3.Text)
txtHeight3 = Convert.ToInt32(Height3.Text)
txtLength4 = Convert.ToInt32(Length4.Text)
txtHeight4 = Convert.ToInt32(Height4.Text)
Dim Area1Total As Integer = txtLength1 * txtHeight1
Dim Area2Total As Integer = txtLength2 * txtHeight2
Dim Area3Total As Integer = txtLength3 * txtHeight3
Dim Area4Total As Integer = txtLength4 * txtHeight4
Area1.Text = Area1Total
Area2.Text = Area2Total
Area3.Text = Area3Total
Area4.Text = Area4Total
Area = Area1Ttotal + Area2Total + Area3Total + Area4Total
编辑:做了一些小的调整,以便你能够拥有最后的区域部分,因为它应该也是如此(因为.Text将使它再次成为一个字符串)。
编辑2:
如果“TotalArea”控件不是用户应填写的字段,而是根据各种长度和高度计算的总面积之和,则代码应如下所示:
Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer
txtLength1 = Convert.ToInt32(Length1.Text)
txtHeight1 = Convert.ToInt32(Height1.Text)
txtLength2 = Convert.ToInt32(Length2.Text)
txtHeight2 = Convert.ToInt32(Height2.Text)
txtLength3 = Convert.ToInt32(Length3.Text)
txtHeight3 = Convert.ToInt32(Height3.Text)
txtLength4 = Convert.ToInt32(Length4.Text)
txtHeight4 = Convert.ToInt32(Height4.Text)
Dim Area1Total As Integer = txtLength1 * txtHeight1
Dim Area2Total As Integer = txtLength2 * txtHeight2
Dim Area3Total As Integer = txtLength3 * txtHeight3
Dim Area4Total As Integer = txtLength4 * txtHeight4
Area1.Text = Area1Total
Area2.Text = Area2Total
Area3.Text = Area3Total
Area4.Text = Area4Total
Area = Area1Ttotal + Area2Total + Area3Total + Area4Total
TotalArea.Text = Area