Blocking user access to controls

jantman
03-07-2006, 10:19 AM
Hello,

I am looking for a way to set controls on a form so that they do not accept any changes.

I tried
Me.Enabled = False

but have two problems:

1) The controls cannot be grayed out, I want them to look normal, just not accept any changes.

2) The form uses a TabControl with two tabs, which I need to remain functional, and I also need the "Cancel" and "Submit" buttons (just normal Button controls) to remain functional.

Any help would be appreciated.

Thanks,
Jason

jo0ls
03-07-2006, 11:30 AM
Me refers to whatever class it is declared in (well, nearly always anyway). So in a Form class, Me refers to that instance of the form. So saying Me.Enabled = false tries to disable the whole form. I'm guessing it disables all the controls on the form, I've never tried it.

So, instead of disabling all the form, you would normally write code to disable just the relevant controls.

If you want to disable textbox1, you can use
TextBox1.Enabled = False

Say we have a form with comboboxes and textboxes for the user to enter data, you could write a sub like this:


Sub EnableDataEntryControls(byval Enable as Boolean)

txtName.Enabled = Enable
txtAddress.Enabled = Enable
cboOrder.Enabled = Enable

End Sub


So then , at the point where you want to disable that set of controls you would call the sub:
EnableDataEntryControls(False)
and it would pass that False on as the variable Enable, so the controls you put in the sub will all get disabled.

Later, you would want them enabled again and would call:
EnableDataEntryControls(True)

Setting Enabled to False doesn't get the effect you require. Textboxes have a Readonly setting, but comboboxes don't.
To keep the white background is difficult, as there is no built in way to do it. (When set to disabled, the backcolor property of the control is locked.)

jantman
03-07-2006, 01:19 PM
Me refers to whatever class it is declared in (well, nearly always anyway). So in a Form class, Me refers to that instance of the form. So saying Me.Enabled = false tries to disable the whole form. I'm guessing it disables all the controls on the form, I've never tried it.

So, instead of disabling all the form, you would normally write code to disable just the relevant controls.

If you want to disable textbox1, you can use
TextBox1.Enabled = False

Say we have a form with comboboxes and textboxes for the user to enter data, you could write a sub like this:


Sub EnableDataEntryControls(byval Enable as Boolean)

txtName.Enabled = Enable
txtAddress.Enabled = Enable
cboOrder.Enabled = Enable

End Sub


So then , at the point where you want to disable that set of controls you would call the sub:
EnableDataEntryControls(False)
and it would pass that False on as the variable Enable, so the controls you put in the sub will all get disabled.

Later, you would want them enabled again and would call:
EnableDataEntryControls(True)

Setting Enabled to False doesn't get the effect you require. Textboxes have a Readonly setting, but comboboxes don't.
To keep the white background is difficult, as there is no built in way to do it. (When set to disabled, the backcolor property of the control is locked.)


There are two problems with this. First, what do I do about combo boxes, check boxes, radio buttons, etc? I need them all to stay the same.

Second, I have literally hundreds of controls on the two tabs. It's quite a b*tch to have to code them all to readonly, or whatever else. Any other ideas?

jo0ls
03-07-2006, 07:04 PM
It's usually a good idea to stick with te default behaviour, as it is windows standard and that is what people expect. If a user has a white textbox and they can't enter text then they will think it is a bug.
Anyway. Controls lie inside a container. A form is a container for controls, so are groupboxes and plenty of other controls. You have tabpages, and they are containers too. You can loop through all the controls in a container, test if they should be changed, and set accordingly:


Sub SetSomeControls(ByVal Enable As Boolean)
For Each c As Control In Me.TabPage1.Controls
If c.Name = "Button1" Then
' we don't want to disable it, do nothing
Else
' all other controls get set to the value of enable
c.Enabled = Enable
End If
Next
End Sub


In this loop c refers to each control on tabpage1 in turn (but not any controls that are in other containers on tabpage1). That code sets every control apart from button1. Instead of using the name property, you could set the Tag poroperty of the controls you want to leave alone in the designer to something and test for that instead. You can set the property on multiple controls by ctrl-Clicking them in the designer, then setting tag.

The only way I can think of to get each control to remain visually the same but not accept input, is to have a custom control for every type of control you have. Each custom control would need to override OnEnabledChanged (or whatever it is called), and remove the call to mybase.OnEnabledChanged. Then you would have to redirect all mouse and key clicks, and stop it getting focus. To do that for each control is a big task. And then you would have to redo the form. Maybe someone else knows a way...

edit: ah hold on, there might be a pinvoke way using EnableWindow

jo0ls
03-07-2006, 07:46 PM
Yeah, there is an api call to disable an item form it's handle.

You need to declare use
imports System.Runtime.Interopservices
and then declare the EnableWindow function:

<DllImport("user32.dll", EntryPoint:="EnableWindow")> _
Private Shared Function EnableWindow(ByVal handle As IntPtr, ByVal bEnable As Boolean) As UInteger
End Function


You can then make a control ignore all input with the function, e.g.:
EnableWindow(Button1.handle, False)
would disable button1.

So you can:

1) Tag all the controls that you want to leave accessable by setting the tag property (which is an object so you can use anything you want as the tag)
2) Make a sub that will loop through all the controls in a tab page. Test if the current control in the loop has its tag set and then use EnableWindow appropriately.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum