如何将文本框中的四个数字相加并在 visual studio 中的单独文本框中显示答案?

How can I add up four numbers from textboxes together and display the answer in a seperate textbox in visual studio?

视觉基础 在我的代码中,我让用户输入四堵墙中每一堵墙的长度和高度。我得到了将长度乘以高度的代码,以计算出每面墙的面积,并在单独的文本框中显示四个区域中的每一个。现在我想将文本框中的四个数字相加,并在单击按钮时在单独的文本框中显示答案。

这是我目前拥有的代码;

    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

结束Class

当我 运行 这段代码时,我得到一个错误提示 "Conversion from string "" to type 'Integer' is not valid."

由于 TextBox 控件将其内容存储为字符串/文本,因此您必须将这些文本值解析为数值,然后才能将其用于计算。

参见以下示例:

txtLength1 = int.Parse(Length1.Text)

编辑: 您还可以考虑使用 int.TryParse method 而不是 int.Parse 来检测用户何时输入了非数字文本。

没有考虑到人们倾向于写除整数以外的其他东西 这可以解决问题:

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

编辑:进行了一些小的调整,以便您能够拥有最后一个 Area 部分,因为它应该也是如此(因为 .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