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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling all controlls - very weird problem 1

Status
Not open for further replies.

MrSandman666

Programmer
Feb 5, 2001
54
0
0
DE
Hello everybody.
Yesterday night I was trying to write a little bit of code to disable/hide all the controls in a form. I did something like this:

dim MyControl as Control
For Each MyCOntrol In frmMain.Controls
MyControl.Visible = false
Next MyControl

I wrote this off the top of my head since the source code is on another computer, so please excuse any typos. Basicaly the code looks correct, right? To me it does. But still, when I try to run the program, the compiler always tells me that MyControl doesn't support the property. What's going on here? I took this right from a book! It should work. Is there something I might have overlooked?

Please! Help!
 
Here is what i use...

Dim i As Integer

For i = 0 To Form1.Controls.Count - 1
Form1.Controls(i).Enabled = False
Next i
Dragnut
 
Oh yeah, to enable the controls use...

Dim i As Integer

For i = 0 To Form1.Controls.Count - 1
Form1.Controls(i).Enabled = true
Next i
Be careful using these because if you
have code that says "Call Command1_click"
then it will not fire.
Dragnut
 
I tried what you had, and it worked...hid all the controls like it should. Dont know why it didnt work for you
Dragnut
 
Some controls do not have the Visible property, Width, Move method etc e.g. Timer, MSComm
Try this in a BAS module and use for any form.
Code:
Sub EnDisable (frm as Form,blnEnable as boolean)
    Dim objCtl as Control
    For Each objCtl in Frm.Controls
       On Error Resume Next
           objCtl.Enabled = blnEnable
       On Error Goto 0
   Next
    Set objCtl = Nothing
End Sub
 
Good point about the conrtols that dont have enabled properties(or whatever property). I must admit, i just put the basics on the form to test it out, probably just didnt get one without the right properties :)
Dragnut
 
Hmm, but when I hae just a couple of controls not supporting Enabled or whatever, should the whole loop fail? I stepped my way through it and the first control encountered is a command button, which, for some strange reason, gives me the above mentioned error. Weird... I do have some non-standard controls, which may not support enabled. For example, there's a treeview and a menu. That might be it. But should the whole loop fail because of that?
 
A command button Enabled or Visible will not fail unless you are trying to set it to something besides True or False. Yes the whole loop (program) fails unless you intercept the error.
 
In general, you cannot set a property of a control that has the focus where the control cannot (then) have the focus. So, setting the [Enabled | Locked | Visible] property of the control which has the focus should cause an error. If the 'routine' is called from the command button and the command button is the first control in the sequence ...


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
???

I have never encountered this limitation. I guess I must be doing something wrong...
 
John, et al. Not doubting you, but this is the err I get. It is similar to others, depending on the operation. Perhaps one of us is doing something different.

Code:
Private Sub Command1_Click()
'    MsFlexGrid1.SetFocus       'Here, At least in Part to Keep from err
    Command1.Enabled = False    'With the comment on the Preceeding Line'
                                'Err 2164 "You Can't disable a control
                                'while it has the focus"
    Command2.Enabled = True
    CountArray = 0
    ArrayAverage MyArray

With the "improvement" of HTML Help, I cannot find the reference to the error, but I have run into this on at least a few occassions. It is also LOGICAL, in that if you "disable" the control which has the focus, it could be difficult to either figure out where you were in the controls collection or to change hte focus to another control.


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Just to get the versions straight: I have VB6, no service pack and I don't get any errors considering focus. Teh only error I get is something along the lines of "object doesn't support this property".
I call the hide proccedure at startup (Form_Load()).
I tried stepping through with the debugger and at the very first element in the controls collection, which is a command button, I get the above mentioned error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top