【问题标题】:How to retrieve values from dynamically created textboxes in VB?如何从 VB 中动态创建的文本框中检索值?
【发布时间】:2014-03-04 13:05:24
【问题描述】:

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

这是我的程序应该如何工作的。

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

我正在使用先到先服务算法。请帮帮我。

这是我的代码: 公共类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

提前致谢!

【问题讨论】:

  • 使用文本框名检索值
  • 在 Process 方法之外尝试 Dim burstbox(15) As TextBox,而不是 Private burstbox() As TextBox。如果您为每个文本框数组执行此操作,您将能够从其他方法访问它们。
  • 怎么样?请进一步解释。 :(

标签: vb.net


【解决方案1】:

如果您创建这些控件并将它们分配给本地数组,您将只能(稍后)通过 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

结束子

【讨论】:

  • 先生!还有一件事。现在我可以访问文本框的值,我很难将值分配给其他文本框。基本上,代码应该像这样工作: BurstTime P1:1 P2:2 P3:3 P4:4 P5:5 || WaitingTime: P1: 0 P2: 1 P3: 3 P4: 6 P5: 10..等待时间(P1)应该从0开始,接下来是waitingtime(P2)= waitingtime(P1)+burstime(P1)..和很快。请先生帮帮我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多