VB6创建OCX启用/禁用内部的所有控件

时间:2012-03-30 17:16:27

标签: vb6 children ocx

我创建了一个几乎充当框架的OCX。我希望能够启用/禁用组件并将所有其他控件放在框架内,也可以禁用。有没有办法实现这个目标?

由于

1 个答案:

答案 0 :(得分:4)

我做了一些实用方法来完成这个以及更多。随意使用并改变它!

Option Explicit

Private Enum ControlProperty
    PropertyEnabled
    PropertyVisible
    PropertyText
End Enum

Public Sub SetEnabledPropertyForAllControlsInFrame(frmForm As Form, strFrameCaption As String, Optional booValue As Boolean)
    SetPropertyForAllControlsInFrame frmForm, strFrameCaption, PropertyEnabled, booValue
End Sub

Public Sub SetVisiblePropertyForAllControlsInFrame(frmForm As Form, strFrameCaption As String, Optional booValue As Boolean)
    SetPropertyForAllControlsInFrame frmForm, strFrameCaption, PropertyVisible, booValue
End Sub

Public Sub ClearAllTextBoxesInFrame(frmForm As Form, strFrameCaption As String, Optional strText As String)
    If IsMissing(strText) Then strText = vbNullString
    SetPropertyForAllControlsInFrame frmForm, strFrameCaption, PropertyText, strText
End Sub

Public Sub ClearAllTextBoxesInForm(frmForm As Form, Optional ExceptInFrame As Frame)
    Dim ctl As Control
    Dim strCaption As String

    If ExceptInFrame Is Nothing Then
        For Each ctl In frmForm.Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = vbNullString
            End If
        Next
    Else
        strCaption = ExceptInFrame.Caption
        ExceptInFrame.Caption = "xdgerviye246123nvasdmnvwe8"
        For Each ctl In frmForm.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl.Container Is Frame Then
                    If Not ctl.Container.Caption = ExceptInFrame.Caption Then
                        ctl.Text = vbNullString
                    End If
                End If
            End If
        Next
        ExceptInFrame.Caption = strCaption
    End If

End Sub

Public Sub ClearAllCheckBoxesInForm(frmForm As Form)
    Dim ctl As Control

    For Each ctl In frmForm.Controls
        If TypeOf ctl Is CheckBox Then
            ctl.Value = vbUnchecked
        End If
    Next

End Sub

Private Sub SetPropertyForAllControlsInFrame(frmForm As Form, strFrameCaption As String, enuControlProperty As ControlProperty, varValue As Variant)
    Dim ctrl As Control
    Dim ctrl2 As Control
    For Each ctrl In frmForm.Controls
        If ctrl.Container.Caption = strFrameCaption Then
            Select Case enuControlProperty
                Case ControlProperty.PropertyEnabled
                    ctrl.Container.Enabled = varValue
                    ctrl.Enabled = varValue
                    If TypeOf ctrl Is TextBox Then
                        ctrl.BackColor = IIf(varValue = True, vbWindowBackground, vbButtonFace)
                    End If
                Case ControlProperty.PropertyVisible
                    ctrl.Container.Visible = varValue
                    ctrl.Visible = varValue
                Case ControlProperty.PropertyText
                    ctrl.Text = varValue
            End Select
            If TypeOf ctrl Is Frame Then
                SetPropertyForAllControlsInFrame frmForm, ctrl.Caption, enuControlProperty, varValue
            End If
        End If
    Next
End Sub