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
protected class BMSLockedControlCollection : List<Windows.Forms.Control>
{
public new void Add(Windows.Forms.Control control)
{
if (!IsLocked(control)) {
SetLocked(control, true);
base.Add(control);
}
}
public new void RemoveAll()
{
for (int i = 0; i <= (this.Count - 1); i++) {
SetLocked(Item(0), false);
base.RemoveAt(0);
}
}
private new void Remove(Windows.Forms.Control control)
{
SetLocked(control, false);
base.Remove(control);
}
public bool IsLocked(Windows.Forms.Control control)
{
if (Control is DevExpress.XtraEditors.BaseEdit) {
return ((DevExpress.XtraEditors.BaseEdit)Control).Properties.ReadOnly;
}
else if (Control is DevExpress.XtraGrid.GridControl) {
return !((DevExpress.XtraGrid.Views.Base.ColumnView)((DevExpress.XtraGrid.GridControl)control).FocusedView).OptionsBehavior.Editable;
}
else if (Control is BaseControl) {
return ((BaseControl)control).IsReadOnly;
}
}
public void SetLocked(Windows.Forms.Control control, bool Locked)
{
if (control is DevExpress.XtraEditors.BaseEdit) {
((DevExpress.XtraEditors.BaseEdit)control).Properties.ReadOnly = Locked;
}
else if (control is DevExpress.XtraGrid.GridControl) {
((DevExpress.XtraGrid.Views.Base.ColumnView)((DevExpress.XtraGrid.GridControl)control).FocusedView).OptionsBehavior.Editable = !Locked;
}
else if (control is BaseControl) {
((BaseControl)control).IsReadOnly = Locked;
}
}
}
private BMSLockedControlCollection _lockedControls;
protected BMSLockedControlCollection LockedControls {
get {
if (_lockedControls == null) {
_lockedControls = new BMSLockedControlCollection();
}
return _lockedControls;
}
set { _lockedControls = value; }
}
public void LockControl(Control control)
{
if (!LockedControls.Contains(control)) {
LockedControls.Add(control);
}
}
public void UnlockControl(Control control)
{
if (LockedControls.Contains(control)) {
LockedControls.Remove(control);
}
}
public void LockAllControls(bool Unlock)
{
LockAllControls(this.Controls, Unlock);
}
public void LockAllControls(ICollection controls, bool Unlock)
{
if (Unlock) {
LockedControls.RemoveAll();
}
else {
foreach (Windows.Forms.Control Control in controls) {
LockedControls.Add(Control);
if (Control.Controls != null && !Control is BaseControl) {
LockAllControls(Control.Controls, Unlock);
LockedControls.Add(Control);
}
}
}
}
Add comment