2

With refactoring some old VB6 code I stumbled upon some really weird behavior. When I try to set the DragMode property of an element of a control array, the compiler tells me 'Method or data member not found' when I separate this control from its array.

See my example below where 'myControl' is nothing else but a User Control with a Textbox in it. And where 'Controls' is a Control Array with myControls in it. The first loop works and the second one doesn't.

Dim i As Integer
Dim ctrl As myControl

For i = 0 To 2
    myControls(i).DragMode = vbAutomatic
Next i

For Each ctrl In myControls
    ctrl.DragMode = vbAutomatic
Next

Update:

With thanks to @wqw I got the for each loop working. The code that works for me now looks something like this:

Dim ctrlExt As VBControlExtender
Dim ctrl As myControl

For Each ctrlExt In myControls

    Set ctrl = ctrlExt

    ctrlExt.DragMode = vbAutomatic
    ctrl.SpecificProperty = "Test"
Next
Vincent
  • 23
  • 4
  • Beware that `Set ctrl = ctrlExt` might fail if your user control is in a separate ActiveX Control (OCX) project and/or when application is compiled. You have to use `Set ctrl = ctrlExt.Object` in this case. – wqw Feb 14 '13 at 15:52

1 Answers1

2

Try Dim ctrl As VBControlExtender

This works

Dim i As Integer
Dim ctrl As VBControlExtender

For Each ctrl In Controls
    ctrl.DragMode = vbAutomatic
Next

For i = MyControls.LBound To MyControls.UBound
    MyControls(i).DragMode = vbAutomatic
Next i
wqw
  • 11,771
  • 1
  • 33
  • 41
  • Thanks for your quick response. There's something I must be missing here. To me it seems as if you've just switched the two loops. The second loop of your example always worked (except this one because you've changed the name of the controlarray) and the first loop of your example still doesn't work because I get a Type mismatch now. The type of the control is 'myControl'. – Vincent Feb 14 '13 at 15:08
  • I've tried changing the name of the Control Array to myControls because I found out that Controls is the name of the collection of controls on a form.... :-/ So that was what I was missing. Only I ended up with the same result. – Vincent Feb 14 '13 at 15:20
  • No.. you were right! It does work. I answered too quickly! I'm sorry! – Vincent Feb 14 '13 at 15:25