Apr
10
2010

Simple Collection to “Lock” Controls using DX Editors

User Rating: / 0
PoorBest 

I have a base form that I inherit from within my App it makes it easy to put in helper functions such as my Bind method and this Control locking mechanism.

Basically I inherit from List(Of Control) then Shadow the Add and Remove functions to alter the properties of the control when being locked and unlocked.

This doesn’t take into account Windows Controls with the Enabled property but could easily be extended to work with Windows controls, (I just haven’t dealt with them in so long I wouldn’t even now how to lock them down these days ;))

    Protected Class BMSLockedControlCollection
        Inherits List(Of Windows.Forms.Control)


        Public Shadows Sub Add(ByVal control As Windows.Forms.Control)
            If Not IsLocked(control) Then
                SetLocked(control, True)
                MyBase.Add(control)
            End If
        End Sub

        Public Shadows Sub RemoveAll()
            For i As Integer = 0 To (Me.Count - 1)
                SetLocked(Item(0), False)
                MyBase.RemoveAt(0)
            Next
        End Sub

        Private Shadows Sub Remove(ByVal control As Windows.Forms.Control)
            SetLocked(control, False)
            MyBase.Remove(control)
        End Sub

        Public Function IsLocked(ByVal control As Windows.Forms.Control) As Boolean
            If TypeOf Control Is DevExpress.XtraEditors.BaseEdit Then
                Return CType(Control, DevExpress.XtraEditors.BaseEdit).Properties.ReadOnly
            ElseIf TypeOf Control Is DevExpress.XtraGrid.GridControl Then
                Return Not CType(CType(control, DevExpress.XtraGrid.GridControl).FocusedView, DevExpress.XtraGrid.Views.Base.ColumnView).OptionsBehavior.Editable
            ElseIf TypeOf Control Is BaseControl Then
                Return CType(control, BaseControl).IsReadOnly
            End If
        End Function

        Public Sub SetLocked(ByVal control As Windows.Forms.Control, ByVal Locked As Boolean)
            If TypeOf control Is DevExpress.XtraEditors.BaseEdit Then
                CType(control, DevExpress.XtraEditors.BaseEdit).Properties.ReadOnly = Locked
            ElseIf TypeOf control Is DevExpress.XtraGrid.GridControl Then
                CType(CType(control, DevExpress.XtraGrid.GridControl).FocusedView, DevExpress.XtraGrid.Views.Base.ColumnView).OptionsBehavior.Editable = Not Locked
            ElseIf TypeOf control Is BaseControl Then
                CType(control, BaseControl).IsReadOnly = Locked
            End If
        End Sub
    End Class

    Private _lockedControls As BMSLockedControlCollection
    Protected Property LockedControls As BMSLockedControlCollection
        Get
            If _lockedControls Is Nothing Then
                _lockedControls = New BMSLockedControlCollection
            End If
            Return _lockedControls
        End Get
        Set(ByVal Value As BMSLockedControlCollection)
            _lockedControls = Value
        End Set
    End Property


    Public Sub LockControl(ByVal control As Control)
        If Not LockedControls.Contains(control) Then
            LockedControls.Add(control)
        End If
    End Sub

    Public Sub UnlockControl(ByVal control As Control)
        If LockedControls.Contains(control) Then
            LockedControls.Remove(control)
        End If
    End Sub

    Public Overloads Sub LockAllControls(ByVal Unlock As Boolean)
        LockAllControls(Me.Controls, Unlock)
    End Sub

    Public Overloads Sub LockAllControls(ByVal controls As ICollection, ByVal Unlock As Boolean)

        If Unlock Then
            LockedControls.RemoveAll()
        Else
            For Each Control As Windows.Forms.Control In controls
                LockedControls.Add(Control)
                If Control.Controls IsNot Nothing AndAlso Not TypeOf Control Is BaseControl Then
                    LockAllControls(Control.Controls, Unlock)
                    LockedControls.Add(Control)
                End If
            Next
        End If
    End Sub

Add comment

Although I believe your free to say what you want, please don't abuse either myself or other peoples, be constructive.


Security code
Refresh

Should AussieALF stay bald?




Results

Latest Comments

My Twitter

Follow me on twitter