尝试通过引用另一个复选框以编程方式设置复选框可见性

时间:2018-05-15 12:17:36

标签: vb.net

如果表格中的字段与前缀为' m'的复选框相同,我试图使表单上的复选框可见。设置为true。 该函数遍历表单上的所有控件,当它到达myCheckbox时,它会找到通过添加“&m;”来控制它的复选框的名称。它的名字。 mControl的值通过复选框在另一个表单上设置。因此,第一个表单在第二个表单的复选框上将显示设置为true或false。

Private Function Display_To_Screen(ByVal DS As DataSet, ByVal Table As String, ByVal Row As String, ByVal ParentControl As Control) As Boolean

    Dim mctl As Object
    Dim ctl As Object

    Try
        For Each ctl In ParentControl.Controls
            If TypeOf ctl Is MyCheckBox Then

                mctl = Get_mFieldName(ctl)

                If IsDBNull(mctl) Then
                    ctl.Visible = False
                Else
                    ctl.Visible = True
                End If

            ElseIf ctl.Controls.Count > 0 Then
                ' this ctl is itself a container so we will call this function 
                ' from within itself.
                Call Display_To_Screen(DS, Table, Row, ctl)
            End If

        Next ctl


Public Function Get_mFieldName(ByVal ctl As Control) As String
    'Gets the field name of the mControlName so can find if that value is TRUE to enable visibility on control

    Get_mFieldName = "m" & Right(ctl.Name, Len(ctl.Name) - 3)
End Function

1 个答案:

答案 0 :(得分:1)

信息不足,我没有足够的观点发表评论并提出问题。

分配该代码是不必要的。这是给你的模板。

只需根据CanMyControlBeVisable()的结果设置可见性。

你在Get_mFieldName()中的逻辑总是会返回一个字符串;它永远不会是NULL。因此,可见性始终为TRUE。

Private Function Display_To_Screen(DS As DataSet, Table As String, Row As String, ParentControl As Control) As Boolean
    Try
        For Each ctl As Control In ParentControl.Controls

            If ctl.Controls.Count Then Display_To_Screen(DS, Table, Row, ctl)

            If Not TypeOf ctl Is MyCheckBox Then Continue For

            ctl.Visible = CanMyControlBeVisable(ctl)

        Next
    Catch ex As Exception
        'Do something
    End Try

    'This part is missing from OP's code sample.

    'Are you returning a value?
    'If not, change this to a Sub()

    'Are you using the parameters "DataSet", "Table", and "Row" somewhere down here?
    'If not, delete them from the function definition. 

End Function

Private Function CanMyControlBeVisable(p_ctl As Control) As Boolean
    'Do your work here
    'Return the result as TRUE or FALSE
End Function