Control Arrays dissasociation

JJKazJr

Newcomer
Joined
Jul 28, 2006
Messages
17
Location
Milwaukee, WI
I have a group of pictureboxes on that I am trying to control using a control array and all seemed beautiful till this situation. The pitureboxes that I am trying to control are on form2 which is called from form1. The first time the form is loaded, all things work properly, but all time after the first any code that uses the control array doesn't affect the form at all. Is this due to it being a different instance of form2, and the control array is still pointing to the items on the first instance? To get this to work will I need to set the control array each time I load form2? I would just like a little explination of what exactly is going on so I don't screw something else up.
 
Control arrays are there for compatability purposes. When developing new applications you should avoid using them.

Regardless, if you get a reference to a control or any instance-specific object on a form and spawn a new form, the references will still point to an object on the old form. If you are using "default instances" of forms (a feature in VB8), this is sure to become a point of confusion. If you close the form, then show it again, a new instance is loaded, and any direct references to any objects on that form must be updated to point to the new form. Any "indirect references" won't need to be updated. For example:
Visual Basic:
Public Sub Test
    'Get a direct reference to the collection of controls on Form2
    Dim testControls As ControlCollection = Form2.Controls
 
    'Clear controls through direct reference to control collection (works)
    testControls.Clear()
    'Clear controls through indirect reference (works)
    Form2.Controls.Clear()
 
    'Create a new Form2
    Form2.Close()
    Form2.Show()
 
    'Clear controls through direct reference to control collection 
    '(WONT work unless you update the reference)
    testControls.Clear()
    'Update the reference.
    testControls = Form2.Controls
    'Now it works.
    testControls.Clear()
    'Clear controls through indirect reference (still works)
    Form2.Controls.Clear()
End Sub
I haven't tested the code, or even tried using default instances in VB, but you should get the idea. Hope it helps.
 
Yes, thank you that was exactly what I was looking for. What you said matched what I had found from screwing with it. The direct/indirect thing is what really cleared it up. I am currious however about your comment:
Control arrays are there for compatability purposes. When developing new applications you should avoid using them.
I have written in vb6 in the past and know the benifit of control arrays, but have been using vba recently. I am now beginning to use vb.net and was looking forward to using control arrays again, however if it is a bad practice I would much rather learn the better way. If you could explain why I shouldn't use them and/or suggest a different means I would appreciate it.
 
They are there for compatability. I believe that they are deprecated. This means that Microsoft no longer supports them. No updates. No bug fixes. Nothing like that. Most importantly, they are not guarunteed to be around in future version. What's more, for these reasons, most people will stop using them, meaning that if you have questions or want to share code with other people they will be unfamiliar with them.

For all those reasons, you should really stay away from anything in the Microsoft.VisualBasic.Compatability namespace. As painful as it might be, the best way to handle dynamic control creation and array-like control access is by manually creating arrays or ArrayListsof controls in code.
 
Oh, I may have misunderstood what you had meant, or stated what I was doing improperly. I am not using the VisualBasic.compatability namespace, I think i made an array of controls. I thought that you were talking about that idea of controling objects like that. I'm sorry. Thank you again for your insite.
 
Well, pardon my assumption that you were using the xxxArray components when you said "Control array."
 
Back
Top