Usercontrol property question

G

Gary Kahrau

I hope this is a simple question.
I have a number of usercontrols placed on a form. The usercontrol has
a public property (DispMachine) within it. When looping through the
main form controls, I get an error (not a member of
system.windows.forms.control). However if I put in the explicit name,
VB is happy.
Is it possible to get to this property dynamically?
Or do I have to create a select case (ugly) structure?


For n = 0 To Me.Controls.Count - 1
If Me.Controls(n).GetType.ToString.EndsWith("MachControl") Then

tmpStr = Me.Controls(n).Tag
tmpStr2 = Me.Controls(n).Name
If InStr(tmpStr, vbReader.Item("machine"),
CompareMethod.Text) Then

Me.Controls(n).DispMachine = tmpStr <==== Error
Me.MachControl1.DispMachine = tmpStr <==== Works

End If

End If
Next
 
A

Armin Zingler

Gary said:
I hope this is a simple question.
I have a number of usercontrols placed on a form. The usercontrol has
a public property (DispMachine) within it. When looping through the
main form controls, I get an error (not a member of
system.windows.forms.control). However if I put in the explicit name,
VB is happy.
Is it possible to get to this property dynamically?
Or do I have to create a select case (ugly) structure?


For n = 0 To Me.Controls.Count - 1
If Me.Controls(n).GetType.ToString.EndsWith("MachControl") Then

Any reason why you don't use

if typeof me.controls(n) is MachControl then

?

What ist the type name of the control class? Did you write different
Usercontrols?

tmpStr = Me.Controls(n).Tag
tmpStr2 = Me.Controls(n).Name
If InStr(tmpStr, vbReader.Item("machine"),
CompareMethod.Text) Then

Me.Controls(n).DispMachine = tmpStr <==== Error
Me.MachControl1.DispMachine = tmpStr <==== Works

End If

End If
Next


The type of the Items property of the Controls collection is 'Control'. Not
every control has this property, thus the message. If 'MachControl' is the
class name:

directcast(Me.Controls(n), MachControl).DispMachine = tmpstr


Armin
 
G

Gary Kahrau

Any reason why you don't use

if typeof me.controls(n) is MachControl then

?

No, I am relitively new to .Net and I base a lot off of code samples
that I find. I will try the typeof.
What ist the type name of the control class? Did you write different
Usercontrols?

No, there is only multiple copies of the same usercontrol.
Windowscontrollibrary1.MachControl <== type name ???
The type of the Items property of the Controls collection is 'Control'. Not
every control has this property, thus the message. If 'MachControl' is the
class name:

directcast(Me.Controls(n), MachControl).DispMachine = tmpstr

I will try this as well.


Thanks,
Gary...
 
A

Armin Zingler

Gary said:
No, there is only multiple copies of the same usercontrol.
Windowscontrollibrary1.MachControl <== type name ???


Yes, that's the type name.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top