Jump to content
Xtreme .Net Talk

Locking controls on form


Recommended Posts

Guest aruser
Posted

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

Guest aruser
Posted

Thanks for the reply.

 

The readOnly property changes the back color of the text control to gray (similar to the enabled false effect).

  • *Gurus*
Posted

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.

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

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.

Guest aruser
Posted

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.

Guest aruser
Posted

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 ?

  • 3 years later...
Posted

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...