question with custom control and for each

  • Thread starter Thread starter hans.werner
  • Start date Start date
H

hans.werner

hi,

I have a little problem with a custom control and for each
(number is an integer)
(the control names are AxCTC1 etc

Dim ctrCTI As AxCTCLib.AxCtc
For Each ctrCTI In Me.Controls <---- error here
Dim indxCTI As String = ctrCTI.Name
If indxCTI = "AxCtc" & number Then
ctrCTI.AssignDevice = number
End If
Next

if i do this it brings up an error in For Each etc.
'System.InvalidCastException'
the conversion is not possible

i dunno what I should write "me.controls" is not allowed for that
control i think. it works for textboxes or anything else. the strange
thing is when i do dim ctrCTI as control it shows up. but in this case
" ctrCTI.AssignDevice = number" doesnt work anymore because its a
special value of this control and not a default ms control .

thanks for help
 
hans.werner said:
hi,

I have a little problem with a custom control and for each
(number is an integer)
(the control names are AxCTC1 etc

Dim ctrCTI As AxCTCLib.AxCtc
For Each ctrCTI In Me.Controls <---- error here
Dim indxCTI As String = ctrCTI.Name
If indxCTI = "AxCtc" & number Then
ctrCTI.AssignDevice = number

End If
Next

if i do this it brings up an error in For Each etc.
'System.InvalidCastException'
the conversion is not possible

i dunno what I should write "me.controls" is not allowed for that
control i think. it works for textboxes or anything else. the
strange thing is when i do dim ctrCTI as control it shows up. but in
this case " ctrCTI.AssignDevice = number" doesnt work anymore because
its a special value of this control and not a default ms control .

thanks for help

Dim ctrCTI As object
'...

For Each ctrCTI In Me.Controls
If TypeOf ctrCTI Is AxCTCLib.AxCtc Then
Dim c as AxCTCLib.AxCtc
Dim indxCTI As String
c = directcast(ctrCTI, AxCTCLib.AxCtc)
indxCTI = c.Name
If indxCTI = "AxCtc" & number Then
c.AssignDevice = number
End If
End If
Next

Not each control is an "AxCTCLib.AxCtc" control. The for-each-next loop
processes all objects and assigns them to ctrCTI. When assigning a control
that's type is not AxCTCLib.AxCtc, an error occurs.
 
Hans,
"> Dim ctrCTI As AxCTCLib.AxCtc
For Each ctrCTI In Me.Controls <---- error here
Dim indxCTI As String = ctrCTI.Name
If indxCTI = "AxCtc" & number Then
ctrCTI.AssignDevice = number
End If
Next
Me.Controls means a control on the form.
You can you AxCTClib.controls

Maybe it helps (I am absolute not sure but you need to have the parent of
the control)

If not,
You can put somewhere in your application
MessageBox.Show(Acontrolname.Parent.Name)
Then you know the name of the parent

I am not sure of this, I never did this but looks logical to me.
Otherwise Herfried willl have a solution, because I think this has something
to do with conversion from VB6 to VB.net.
On the otherhand when it not works like I said, you can to speed it up and
it is upgrading ask this in the special newsgroup for upgrading.
Microsoft.public.dotnet.languages.vb.upgrade

Cor
 
Guten Morgen Hans,

Me.Controls is a Collection of Controls. If you get an error it's because
one of the Controls in Me is not an AxCTCLib.AxCtc.

You must either ensure that Me.Controls only contains AxCTCLib.AxCtcs. Or
you must use Object for your ForEach and check the type:

If ctrCTI Is AxCTCLib.AxCtcs Then
... Name ... Number ..
End If

Regards,
Fergus
MVP [Windows Keyboard, PC Power Switch]
 
Back
Top