Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locking a check box

Status
Not open for further replies.

SurvivorTiger

Programmer
Jul 9, 2002
265
US
hey everybody
Is there anyway that i can lock a check box so if you click on it, nothing happenes, but i still want it to be enabled and i don't want it to be grayed either.
thanks in advance
 
if you don't want anything to happen when they click on the check box then its a simple as not putting any code for that check box
 
Create a module level variable to hold the state of the box (vbChecked,vbUnChecked). On the click event of the checkbox set the value equal to your variable.
 
More help please...
It doesn't matter if you write any code or no, if you just create a check box and not write any code, still when you run the program, you would be able to click on check box and uncheck or check it.
 
As kbuc says:
In the declarations section of your form put:

Code:
Public IsChecked As Integer

In the Form_Load event put:
Code:
IsChecked = 1
Check1.Value = 1

In the Check1_Click event put:
Code:
Check1.Value = IsChecked

I've tried it and it works as advertised. The checkbox is enabled, but but always shows as checked. Let me know if this helps

Check out FAQ222-2244
'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
This worked, thanks.
but there is a little problem, i have this command button that when you click on it, it saves a file and does some other stuff and it also makes the Check1.Value = vbChecked, Now this code(in below)prevents the check box from being checked by that command button.

If Check1.Value = 1 then
IsChecked = 1
Else
IsChecked = 0
End If

Is there anything else i can do?
thanks for helping
 
Draw a frame on your form larger than the checkbox. Left-click on the checkbox. Right-click it and select Cut. Left-click the frame. Right-click the frame and select Paste. Now make Enabled=false and BorderStyle=none for the frame in the properties window or at run-time. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
And, you can always use the Tag property as a Pseudo Locked property:

Check1.Tag = "LOCKED"

Private Sub Check1_Validate(Cancel As Boolean)
If Check1.Tag = "LOCKED" Then
Cancel = True
End If
End Sub

You can extend this method by adding additional property proceedures:

Private Property Let CheckBoxLocked(ctrl As Control, Value As Boolean)
If TypeOf ctrl Is CheckBox Then
If Value Then
ctrl.Tag = "LOCKED"
Else
ctrl.Tag = vbNullString
End If
End If
End Property

Private Property Get CheckBoxLocked(ctrl As Control) As Boolean
If TypeOf ctrl Is CheckBox Then

If ctrl.Tag = "LOCKED" Then
CheckBoxLocked = True
Else
CheckBoxLocked = True
End If

End If
End Property

And set it like this:

CheckBoxLocked(Check1)=True

Or Check it's value like this:

Debug.Print CheckBoxLocked(Check1)

To go one step even further you could also drop the last two property proceedures into a MODULE, changing them to PUBLIC, and then use this through-out the propram for any check box.....
Want to add other properties using the TAG property? Then do so using a comma delimiter to seperate the items in the Tag property: Tag = LOCKED, BOLD, BACKGREY
And the using InStr/Replace functions, or better yet, the Split/Filter/Join functions you can find if a value exists, remove a value, or add a value.

Of course you could just simply create a user control and add the missing property/method....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top