如何从VB中动态创建的文本框中检索值?

时间:2014-03-04 13:05:24

标签: vb.net

请问如何从动态创建的文本框中检索值?

以下是我的计划应该如何运作。

程序将询问用户应创建多少个文本框。创建后,用户将输入值到这些文本框(突发时间文本框)。然后,当单击一个按钮时,将从这些文本框中获取值,这些值将用于计算等待时间和周转时间,它们将分别显示在文本框等待时间和周转时间文本框中。

我正在使用First Come First Serve算法。请帮帮我。

这是我的代码: 公共类Form6

Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Public Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    'creates burst time textboxes

    GroupBox3.Visible = True
    Button1.Visible = True
    Dim cnt As Integer
    Dim burstbox(15) As TextBox
    For cnt = 0 To Val(TextBox1.Text) - 1

        burstbox(cnt) = New TextBox
        With burstbox(cnt)
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 80
            .Top = .Height * cnt + 50
            .Visible = True
            .Tag = cnt
            .Text = ""
            .Name = "burst" & cnt
            .Location = New Point(90, 170 + (cnt * 25))

        End With
    Next cnt

    'creates waiting time textboxes
    Dim cnt2 As Integer
    Dim waitbox(15) As TextBox
    For cnt2 = 0 To Val(TextBox1.Text) - 1

        waitbox(cnt2) = New TextBox
        With waitbox(cnt2)
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 80
            .Top = .Height * cnt2 + 50
            .Visible = True
            .Tag = cnt2
            .Text = ""
            .Name = "wait" & cnt2
            .Location = New Point(200, 170 + (cnt2 * 25))
            .ReadOnly = True
        End With
    Next cnt2

    'creates turnaround time textboxes
    Dim cnt3 As Integer
    Dim turnaroundbox(15) As TextBox
    For cnt3 = 0 To Val(TextBox1.Text) - 1

        turnaroundbox(cnt3) = New TextBox
        With turnaroundbox(cnt3)
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 80
            .Top = .Height * cnt3 + 50
            .Visible = True
            .Tag = cnt3
            .Text = ""
            .Name = "turn" & cnt3
            .Location = New Point(310, 170 + (cnt3 * 25))
            .ReadOnly = True
        End With
    Next cnt3

    'process labels here
    Dim cnt4 As Integer
    Dim processlabel(15) As Label
    For cnt4 = 0 To Val(TextBox1.Text) - 1

        processlabel(cnt4) = New Label
        With processlabel(cnt4)
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 80
            .Top = .Height * cnt4 + 50
            .Visible = True
            .Tag = cnt4
            .Text = "P" & cnt4 + 1
            .Name = "label" & cnt4
            .Location = New Point(30, 170 + (cnt4 * 25))
            .ForeColor = Color.DodgerBlue

        End With
    Next cnt4

End Sub

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您创建这些控件并将它们分配给本地数组,您将只能通过form.controls访问它们(稍后)。将它们存储在模块化(表单级全局)变量中可能更好。请尝试使用此代码:

Private burstbox As New List(of TextBox)

Public Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
   'creates burst time textboxes

   GroupBox3.Visible = True
   Button1.Visible = True
   Dim cnt As Integer
   For cnt = 0 To Val(TextBox1.Text) - 1

       burstbox.add(New TextBox)
       With burstbox(cnt)
          .Parent = Me
          .Left = 0
          .Height = 13
          .Width = 80
          .Top = .Height * cnt + 50
          .Visible = True
          .Tag = cnt
          .Text = ""
          .Name = "burst" & cnt
          .Location = New Point(90, 170 + (cnt * 25))
       End With
   Next cnt

  'etc

End Sub