在运行时获取多个创建的控件的值VB.NET

时间:2016-03-11 08:03:03

标签: vb.net

我可以获得在运行时创建的单个控件的值,该控件是DateTimePicker,但我无法获得多个控件的值。我该怎么做?

代码:

这是我在运行时每次点击都添加DateTimePicker的地方。

Private Sub btnAddTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTime.Click
    Dim Time As New DateTimePicker()
    Dim count As Integer = GroupBox1.Controls.OfType(Of DateTimePicker)().ToList().Count
    Time.Location = New Point(9, (23 * count) + 22)
    Time.Size = New Size(150, 20)
    Time.Format = DateTimePickerFormat.Time
    Time.ShowUpDown = True
    Time.Name = "DateTimePicker" & (count + 1)
    GroupBox1.Controls.Add(Time)
End Sub

这就是我获得控件值的地方。

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim i As Integer = 0
    For Each cntrl In Form3.GroupBox1.Controls
        Dim dt As DateTimePicker = Form3.GroupBox1.Controls.Item("DateTimePicker" & (i + 1))
        If DateTime.Now.ToString = dt.Value Then
            MsgBox("P")
        End If
    Next
End Sub

如果我只在代码中指定了它,我只能得到创建控件的值,我想我无法得到控件的正确Name。我试图让程序在Timer滴答时以另一种形式读取控件的每个值。帮助

1 个答案:

答案 0 :(得分:3)

试试这个:

    For Each cntrl In Form3.GroupBox1.Controls
        If TypeOf cntrl Is DateTimePicker Then
            If DateTime.Now.ToString = cntrl.Value Then
                MsgBox("P")
            End If
        End If
    Next