Guest aruser Posted July 4, 2002 Posted July 4, 2002 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 Quote
*Gurus* Derek Stone Posted July 4, 2002 *Gurus* Posted July 4, 2002 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 Quote Posting Guidelines
Guest aruser Posted July 8, 2002 Posted July 8, 2002 Thanks for the reply. The readOnly property changes the back color of the text control to gray (similar to the enabled false effect). Quote
*Gurus* divil Posted July 8, 2002 *Gurus* Posted July 8, 2002 Make yourself a class that derives from TextBox, and eat the keypress notifications: 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest aruser Posted July 8, 2002 Posted July 8, 2002 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. Quote
*Gurus* divil Posted July 8, 2002 *Gurus* Posted July 8, 2002 Why not do what I suggested instead? It would be much easier. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest aruser Posted July 10, 2002 Posted July 10, 2002 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. Quote
*Gurus* divil Posted July 10, 2002 *Gurus* Posted July 10, 2002 I'd say about 5 minutes work. Find/Replace. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest aruser Posted July 10, 2002 Posted July 10, 2002 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 ? Quote
jcrcarmo Posted December 14, 2005 Posted December 14, 2005 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 :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.