Locking controls on form

  • Thread starter Thread starter aruser
  • Start date Start date
A

aruser

Guest
I'm trying to find a way to lock controls (on RunTime) from editing.

I have an application with lots of forms. each form contain standard controls only (TextBoxs, Check boxes, Radio options etc.)
I'm searching for a way to lock all controls on a form when it is opened (If for example, the current user has a ReadOnly access to my application).

What happned to the old-good VB6 Locked property ?

Thanks
 
Not every control in Visual Basic 6 has a .Locked property. As a matter of fact very few of them do. However you may want to take a look at the .ReadOnly property in .NET.

-CL
 
Thanks for the reply.

The readOnly property changes the back color of the text control to gray (similar to the enabled false effect).
 
Make yourself a class that derives from TextBox, and eat the keypress notifications:

Code:
Friend Class clsLockedTextBox
    Inherits System.Windows.Forms.TextBox

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
    End Sub
End Class

I just tried it, and it works fine. You can't copy text from it with Ctrl-C, but you can code an exception for that.
 
Great !!

It will not be easy .. I have several types of controls on each form, but I can write a loop which will add an handler to KeyPress event to each one of the controls. (Other events to cancel input if the KeyPress event is not supported for the control type).

As I said, Not Easy ... But possible !!

Thanks.
 
Because the forms are already filled with standard text boxes.
Any way,
I will check what is the cost to replace all controls all over the application.
 
I tried to create my own (inherited) control but now I can not see the controls during designtime.

Is thare something i can do to display them ?
 
Locking controls on a form - VB 2005

Greetings from Brazil! Here's a simple Public Sub that locks all TextBox and MaskedTextBox controls on a form, switching their ReadOnly property. Also, it keeps the controls BackColor white. I hope it helps:



Public Sub LockForm()
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If CType(ctl, TextBox).ReadOnly = False And CType(ctl, MaskedTextBox).ReadOnly = False Then
CType(ctl, TextBox).ReadOnly = True
CType(ctl, TextBox).BackColor = Color.White
CType(ctl, MaskedTextBox).ReadOnly = True
CType(ctl, MaskedTextBox).BackColor = Color.White
End If
Next
End Sub



Public Sub UnlockForm()
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If CType(ctl, TextBox).ReadOnly = True And CType(ctl, MaskedTextBox).ReadOnly = True Then
CType(ctl, TextBox).ReadOnly = False
CType(ctl, TextBox).BackColor = Color.White
CType(ctl, MaskedTextBox).ReadOnly = False
CType(ctl, MaskedTextBox).BackColor = Color.White
End If
Next
End Sub



Best Regards,

JC :)
 
Back
Top