如何使用循环变量作为文本框名称的一部分在循环中调用TextBoxes [英] How to call up TextBoxes in a loop using the loop variable as part of the Textbox name
问题描述
我正在尝试使用来自四个文本框的数据,并创建要放在另一个表单上的其他四个文本框中的值。我需要为八个团队做这个,所以需要循环八次。
我的代码如下;
Dim Handi(3)作为整数
Dim minHC作为整数
Dim A,B,C,D As Double
Dim HandC1,HandC2,HandC3,HandC4 As TextBox
Dim HandCName1,HandCName2,HandCName3,HandCName4 As String
Dim SA1,SA2,SA3,SA4 As TextBox
Dim SAName1,SAName2,SAName3,SAName4 As String
MatchDetailsFrm.Show()
'循环通过八支球队。
对于i = 1到8
'为每个包含数据的团队生成四个文本框的名称。
HandCName1 =HC&((2 * i) - 1 )
HandCName2 =HC&(2 * i)
HandCName3 =VHC&((2 * i) - 1)
HandCName4 =VHC&(2 * i)
Ha ndC1 = CType(Me.Controls(HandCName1),TextBox)
HandC2 = CType(Me.Controls(HandCName2),TextBox)
HandC3 = CType(Me.Controls( HandCName3),TextBox)
HandC4 = CType(Me.Controls(HandCName4),TextBox)
'生成文本框的名称加载计算的数据。
SAName1 =TextBox&(61 +(4 * i))
SAName2 =TextBox&(63 +(4 * i) ))
SAName3 =TextBox&(30 +(4 * i))
SAName4 =TextBox&(32 +(4 * i))
SA1 = CType(MatchDetailsFrm.Controls(SAName1),TextBox)
SA2 = CType(MatchDetailsFrm.Controls(SAName2),TextBox)
SA3 = CType(MatchDetailsFrm.Controls(SAName3),TextBox)
SA4 = CType(MatchDetailsFrm.Controls(SAName4),TextBox)
'用数据加载数组Handi()从文本盒子。
Handi(0)= Val(HandC1.Text)
Handi(1)= Val(HandC2.Text)
Handi( 2)= Val(HandC3.Text)
Handi(3)= Val(HandC4.Text)
'设定最小值
minHC = 100
'查找数组中的最低值
For Each Integer In Handi >
minHC = Math.Min(minHC,元素)
下一页
'计算新值并加载到文本框在MatchDetailsFrm上
A = Math.Round((HandC1.Text - minHC)* 0.75,MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA1.Text = A
B = Math.Round((HandC2.Text - minHC)* 0.75,MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA2.Text = B
C = Math .Round((HandC3.Text - minHC)* 0.7 5,MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA3.Text = C
D = Math.Round((HandC4.Text - minHC)* 0.75,MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA4.Text = D
下一页
但是我得到了这个编码错误;
当我尝试用这行代码设置文本框时,'SA1'不是MatchDetailsFrm'
的成员;
MatchDetailsFrm.SA1.Text = A
有人可以帮我吗?
Dave
I am trying to use data from four textboxes and create the values to be put in four other textboxes on another form. I need to do this for eight teams so need to loop through eight times.
My code is as follows;
Dim Handi(3) As Integer
Dim minHC As Integer
Dim A, B, C, D As Double
Dim HandC1, HandC2, HandC3, HandC4 As TextBox
Dim HandCName1, HandCName2, HandCName3, HandCName4 As String
Dim SA1, SA2, SA3, SA4 As TextBox
Dim SAName1, SAName2, SAName3, SAName4 As String
MatchDetailsFrm.Show()
' loop through each of the eight teams.
For i = 1 To 8
'Generate the names for the four textboxes per team containing data.
HandCName1 = "HC" & ((2 * i) - 1)
HandCName2 = "HC" & (2 * i)
HandCName3 = "VHC" & ((2 * i) - 1)
HandCName4 = "VHC" & (2 * i)
HandC1 = CType(Me.Controls(HandCName1), TextBox)
HandC2 = CType(Me.Controls(HandCName2), TextBox)
HandC3 = CType(Me.Controls(HandCName3), TextBox)
HandC4 = CType(Me.Controls(HandCName4), TextBox)
'Generate the names for the text boxes to load the calculated data.
SAName1 = "TextBox" & (61 + (4 * i))
SAName2 = "TextBox" & (63 + (4 * i))
SAName3 = "TextBox" & (30 + (4 * i))
SAName4 = "TextBox" & (32 + (4 * i))
SA1 = CType(MatchDetailsFrm.Controls(SAName1), TextBox)
SA2 = CType(MatchDetailsFrm.Controls(SAName2), TextBox)
SA3 = CType(MatchDetailsFrm.Controls(SAName3), TextBox)
SA4 = CType(MatchDetailsFrm.Controls(SAName4), TextBox)
'Load the array Handi() with the data from the textboxes.
Handi(0) = Val(HandC1.Text)
Handi(1) = Val(HandC2.Text)
Handi(2) = Val(HandC3.Text)
Handi(3) = Val(HandC4.Text)
'Set minimum value
minHC = 100
'Find lowest value in the array
For Each element As Integer In Handi
minHC = Math.Min(minHC, element)
Next
'Calculate the new values and load onto the textboxes on the MatchDetailsFrm
A = Math.Round((HandC1.Text - minHC) * 0.75, MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA1.Text = A
B = Math.Round((HandC2.Text - minHC) * 0.75, MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA2.Text = B
C = Math.Round((HandC3.Text - minHC) * 0.75, MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA3.Text = C
D = Math.Round((HandC4.Text - minHC) * 0.75, MidpointRounding.AwayFromZero)
MatchDetailsFrm.SA4.Text = D
Next
However I get this coding error;
'SA1' is not a member of MatchDetailsFrm'
when I try to set the textbox with this line of code;
MatchDetailsFrm.SA1.Text = A
Can anybody help me here?
Dave
推荐答案
我已经通过创建一个文本框数组并使用数组中的循环参数来解决这个问题。
即Dim textBoxes()As TextBox = {TextBox1,TextBox2}
然后在循环中加载数据,其中i = 1到8为
textBoxes((4 * i) - 1).Text = D
I have sorted this problem by creating an array of textboxes and use the loop parameter within the array.
ie Dim textBoxes() As TextBox = {TextBox1, TextBox2}
Then loading data in the loop where i = 1 to 8 as
textBoxes((4 * i) - 1).Text = D
这篇关于如何使用循环变量作为文本框名称的一部分在循环中调用TextBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!