使用VB Net中的变量更快地计算?

时间:2018-12-25 11:36:15

标签: vb.net

我可以将代码缩短一些吗?直到最后一个txtDrawA1在每次计算后将值更改为+ 1,X1更改为每个+1的值(X1到X15-表示txtDrawA1-到txtDrawO1),X16-30表示txtDrawA2到txtDrawO2,X31-X45表示txtDrawA3到txtDrawO3,依此类推。

Private Sub txtSumAO()
    Dim X1 = txtDrawA1.Text
    Dim X2 = txtDrawB1.Text
    Dim X3 = txtDrawC1.Text
    Dim X4 = txtDrawD1.Text
    Dim X5 = txtDrawE1.Text
    Dim X6 = txtDrawF1.Text
    Dim X7 = txtDrawG1.Text
    Dim X8 = txtDrawH1.Text
    Dim X9 = txtDrawI1.Text
    Dim X10 = txtDrawJ1.Text
    Dim X11 = txtDrawK1.Text
    Dim X12 = txtDrawL1.Text
    Dim X13 = txtDrawM1.Text
    Dim X14 = txtDrawN1.Text
    Dim X15 = txtDrawO1.Text
    TextBox16.Text = Val(X1) + Val(X2) + Val(X3) + Val(X4) + Val(X5) + Val(X6) + Val(X7) + Val(X8) + Val(X9) + Val(X10) + Val(X11) + Val(X12) + Val(X13) + Val(X14) + Val(X15)
    Dim X16 = txtDrawA2.Text
    Dim X17 = txtDrawB2.Text
    Dim X18 = txtDrawC2.Text
    Dim X19 = txtDrawD2.Text
    Dim X20 = txtDrawE2.Text
    Dim X21 = txtDrawF2.Text
    Dim X22 = txtDrawG2.Text
    Dim X23 = txtDrawH2.Text
    Dim X24 = txtDrawI2.Text
    Dim X25 = txtDrawJ2.Text
    Dim X26 = txtDrawK2.Text
    Dim X27 = txtDrawL2.Text
    Dim X28 = txtDrawM2.Text
    Dim X29 = txtDrawN2.Text
    Dim X30 = txtDrawO2.Text
    TextBox17.Text = Val(X16) + Val(X17) + Val(X18) + Val(X19) + Val(X20) + Val(X21) + Val(X22) + Val(X23) + Val(X24) + Val(X25) + Val(X26) + Val(X27) + Val(X28) + Val(X29) + Val(X30)
    Dim X31 = txtDrawA3.Text
    Dim X32 = txtDrawB3.Text
    Dim X33 = txtDrawC3.Text
    Dim X34 = txtDrawD3.Text
    Dim X35 = txtDrawE3.Text
    Dim X36 = txtDrawF3.Text
    Dim X37 = txtDrawG3.Text
    Dim X38 = txtDrawH3.Text
    Dim X39 = txtDrawI3.Text
    Dim X40 = txtDrawJ3.Text
    Dim X41 = txtDrawK3.Text
    Dim X42 = txtDrawL3.Text
    Dim X43 = txtDrawM3.Text
    Dim X44 = txtDrawN3.Text
    Dim X45 = txtDrawO3.Text
    TextBox18.Text = Val(X31) + Val(X32) + Val(X33) + Val(X34) + Val(X35) + Val(X36) + Val(X37) + Val(X38) + Val(X39) + Val(X40) + Val(X41) + Val(X42) + Val(X43) + Val(X44) + Val(X45)

1 个答案:

答案 0 :(得分:0)

您可以通过计算文本框的名称来确定它们的名称。您可以使用Me.Controls(controlName)引用表单上的控件,如下所示:

For y = 1 To 3
    Dim sum = 0
    For x = 1 To 15
        Dim tbName = "txtDraw" & Chr(64 + x) & y
        sum += CInt(Me.Controls(tbName).Text)
    Next

    Me.Controls("TextBox" & (15 + y)).Text = sum.ToString()

Next

Chr函数返回与某个值对应的字符,例如,“ A”为65,“ B”为66,and so on