迭代非顺序命名的文本框

时间:2012-11-19 18:58:18

标签: vb.net telerik

我在屏幕上有44个文本框(准确地说,RadNumericTextBoxes,但这不是密切相关的)。它们遵循无法以编程方式复制的通用命名模式(rntb_ [NameOfDBField])。

如何为每个名称为^ = rntb_的控件设置.ValueNothing?我尝试了以下方法:

Private Sub ClearValues()
    For Each c as Control in Controls
        If TypeOf c Is RadNumericTextBox Then
            TryCast(c, RadNumericTextBox).Value = Nothing
        End If
    Next
End Sub

但是,Controls.Count = 1并且只包含母版页的名称。

我是否需要将参数传递给Controls,或者我是否需要完全执行其他操作?这是“仅”44个文本框,所以我可以手动清除每个文本框,但如果可能,我宁愿以编程方式进行。

1 个答案:

答案 0 :(得分:1)

如果RadNumericTextBox位于Form上而不是容器中,那么

Private Sub ClearValues()
    For Each c As Control In Me.Controls
        If TypeOf c Is RadNumericTextBox Then
            Dim rntb = DirectCast(c, RadNumericTextBox)
            If rntb.Name.StartsWith("rntb_") Then
                rntb.Value = Nothing
            End If
        End If
    Next
End Sub

但如果他们在GroupBox1中,那么您可以用Me.Controls替换上面的GroupBox1.Controls

代码中的End For是什么? For..Next循环在VB.NET的主体末尾有Next