如何通过多个类似的文本框使用循环?

时间:2014-11-19 16:11:34

标签: asp.net-mvc vb.net

所以我知道这很邋,,但我有很多事情要做一个简单的三角测量程序(2d)而且我很新。

用户在textboxes中输入值 - " aft1.text,ain1.text,aft2.text,ain2.text,bft1.text,bin1.text,bft2.text,bin1.text。"对于需要三角测量的许多点,这种情况持续存在。我希望能够通过这些类似的文本框运行循环,并在它们上运行一个函数。

因此,对于以abft + i和ain + i开头的每个文本框,运行inches函数来创建a1。

同样创建b1。

然后根据创建的xcoord / ycoord函数在图表上绘制另一个循环来绘制+ i和b + i。

    Dim a1 As Double = inches(aft1.Text, ain1.Text)
    Dim b1 As Double = inches(bft1.Text, bin1.Text)

    If a1 <> 0 And b1 <> 0 Then
        Dim targetpoint As Int32
        targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a1, b1)), ((ycoord(a1, b1))))
        Chart1.Series("Drawing").Points.Item(targetpoint).Label = "1"
    End If

    Dim a2 As Double = inches(aft2.Text, ain2.Text)
    Dim b2 As Double = inches(bft2.Text, bin2.Text)

    If a2 <> 0 And b2 <> 0 Then
        Dim targetpoint As Int32
        targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a2, b2)), ((ycoord(a2, b2))))
        Chart1.Series("Drawing").Points.Item(targetpoint).Label = "2"
    End If


    Dim a3 As Double = inches(aft3.Text, ain3.Text)
    Dim b3 As Double = inches(bft3.Text, bin3.Text)

    If a3 <> 0 And b3 <> 0 Then
        Dim targetpoint As Int32
        targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a3, b3)), ((ycoord(a3, b3))))
        Chart1.Series("Drawing").Points.Item(targetpoint).Label = "3"
    End If

1 个答案:

答案 0 :(得分:1)

如果我理解正确,可以通过创建Sub并使用英寸计算和标签文本调用它来轻松推广此代码:oord可以访问(在范围内)函数:

Triangulate(inches(aft1.Text, ain1.Text), inches(bft1.Text, bin1.Text), 1)
Triangulate(inches(aft2.Text, ain2.Text), inches(bft2.Text, bin2.Text), 2)
Triangulate(inches(aft3.Text, ain3.Text), inches(bft3.Text, bin3.Text), 3)

修改-2

虽然我已经测试了以下几点,但由于我没有你的图表属性,也没有英寸,xcoord和ycoord函数,我无法完全测试它,所以试试看,让我知道它是怎么回事。 / p>

Sub TriangulateAll()
    Try

        Dim aft As New SortedList(Of String, TextBox)
        Dim ain As New SortedList(Of String, TextBox)
        Dim bft As New SortedList(Of String, TextBox)
        Dim bin As New SortedList(Of String, TextBox)

        For Each ctl As Control In Controls
            If TypeOf (ctl) Is TextBox Then
                Select Case ctl.Name.Substring(0, 3)
                    Case "aft"
                        aft.Add(ctl.Name, ctl)
                    Case "ain"
                        ain.Add(ctl.Name, ctl)
                    Case "bft"
                        bft.Add(ctl.Name, ctl)
                    Case "bin"
                        bin.Add(ctl.Name, ctl)
                End Select
            End If
        Next
        Dim a As New List(Of Double)
        Dim b As New List(Of Double)

        For Each kvp_aft As KeyValuePair(Of String, TextBox) In aft
            For Each kvp_ain As KeyValuePair(Of String, TextBox) In ain
                a.Add(inches(kvp_aft.Value.Text, kvp_ain.Value.Text))
            Next
        Next
        For Each kvp_bft As KeyValuePair(Of String, TextBox) In bft
            For Each kvp_bin As KeyValuePair(Of String, TextBox) In bin
                b.Add(inches(kvp_bft.Value.Text, kvp_bin.Value.Text))
            Next
        Next
        For i As Int16 = 0 To aft.Count - 1
            Triangulate(a(i), b(i), i.ToString())
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

修改 使用以下代码将3个调用替换为Triangulate,以完全概括Triangulation例程。请注意,此例程期望每个TextBox控件的数量相同:aft,ain,bft,bin。我没有测试过这个,但它应该可以工作。

    Dim aft As List(Of TextBox) = Nothing
    Dim ain As List(Of TextBox) = Nothing
    Dim bft As List(Of TextBox) = Nothing
    Dim bin As List(Of TextBox) = Nothing

    For Each ctl As Control In Controls
        If TypeOf (ctl) Is TextBox Then
            Select Case ctl.Name.Substring(0, 2)
                Case "aft"
                    aft.Add(ctl)
                Case "ain"
                    ain.Add(ctl)
                Case "bft"
                    bft.Add(ctl)
                Case "bin"
                    bin.Add(ctl)
            End Select
        End If
    Next
    aft.Sort()
    ain.Sort()
    bft.Sort()
    bin.Sort()

    For i As Int16 = 0 To aft.Count - 1
        Dim a As Double = inches(aft.Item(i).Text, ain.Item(i).Text)
        Dim b As Double = inches(bft.Item(i).Text, bin.Item(i).Text)
        Triangulate(a, b, i.ToString())
    Next

   Sub Triangulate(a As Double, b As Double, LabelValue As String)
       If a <> 0 And b <> 0 Then
           Dim targetpoint As Int32
           targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a, b)), ((ycoord(a, b))))
           Chart1.Series("Drawing").Points.Item(targetpoint).Label = LabelValue
       End If
   End Sub