2

我的代码:

Rs.Open("Select * From Notifications",Con)
If Not Rs.EOF Then
   For i=0 to Rs.RecordCount -1
     Dim Label As New Label
     With Label
        .Name = String.Format("Label_{0}",Rs("Id").Value.ToString)
        .Text = Rs("Notification").Value.ToString
     End With
     If Not Panel.Controls.Contains(Label) Then
        Panel.Control.Add(Label)
     End If
     Rs.MoveNext()
   Next
End If

但这总是将控制添加到面板,以下代码行未正确执行: (If Not Panel.Controls.Contains(Label) Then)

此代码适用于计时器。

4

2 回答 2

4
Function CntrlExistsIn(ctrlName as String, parent as Control) as Boolean
    Dim bResult as Boolean = False

    For Each elem as Control In parent.Controls
        If elem.Name = ctrlName Then
            bResult = True
            Exit For
        End If
    Next

    Return bResult
End Function

上面的功能是检查Panel中是否存在control(label)。

于 2013-01-23T10:12:55.393 回答
1

还有另一种方法可以减少您的代码,如下所示

 If panel1.Controls.Find(Label.Name, True).Length = 0 Then
    panel1.Control.Add(Label)
 End If  

Controls.Find(controlName,True/False)------ True/False用于检入指定控件的子控件

于 2013-01-23T11:13:29.427 回答