I am trying to iterate through all the controls in a window, enabling/disabling ones whose parent is a specific control. The following is in a much simplified version of a method which takes the parentControl and value (True/False) as parameters:
Dim i As Integer
Dim ctl As Control
For i = 0 to winMain.ControlCount - 1
ctl = winMain.Control(i)
If ctl.Parent = parentControl Then <------ Gives error: "Type "Control" has no member named "Parent"
ctl.Enabled = value <------ Gives error: "Type "Control" has no member named "Enabled"
I’m sure that I’m making a very stupid mistake here, but I’ve looked through the documentation and can’t find an example to explain what it might be.
I think you’ll need to see if the control is a RectControl first and if it is wrap it. So something like this:
[code]ctl = winMain.Control(i)
if ctl isa rectControl then
If rectcontrol(ctl).Parent IS parentControl Then
rectcontrol(ctl).Enabled = value
If memory serves, container controls show up was something completely different so if you’re using them you might need to do some modifications.
You might also need to use the IS comparator above.
RectControl has a Parent property, but not Control. You should check if the Control returned from the array is a RectControl and then cast and check its Parent property. I’m thinking something like this:
If ctl IsA RectControl Then
If RectControl(ctl).Parent IsA parentControl Then
RectControl(ctl).Enabled = value
Thanks Bob and Paul, that solved my basic problem and got me almost what I needed.
I’m actually doing something that is (strictly) not permitted - like embedding window controls on a container control [Paul, don’t yell at me ], so
If rectcontrol(ctl).Parent Is parentControl Then doesn’t quite work, but I think that I have a workaround. At some point I may strike up a conversation on the correct way to build generalized controls which can be used as containers (similar to group boxes, but with more bells and whistles).
shouldn’t it be?
if rectControl( ctl ).parent <> nil and rectcontrol( ctl ).Parent isa rectControl then
That’s exactly right Sam. But that brings up an interesting question (that I could certainly test in less than 30 seconds, but it’s more interesting to ask here…), does Xojo stop evaluation of a multi stage logic statement when it becomes False? I’ve always coded it as:
if rectControl( ctl ).parent <> nil Then
If rectControl( ctl ).Parent isa rectControl then
This precludes a “Nil Object Exception” error when rectControl is Nil. If it does (stop evaluating the logic statement) then I’m impressed!
Yes. It stops evaluating early.