?? Access Properties of a Custom Object added dynamically

G

Guest

I have been searching the www for 3 hours. I have a Custom Object with a
Public Property of "ControlRes" I have a win form that I dynamically spray
this control onto with the name of "Image1" at run time I can not access the
Image1.ControlRes property. I have tried using for each Control in
Countrols.count and looking for the name of the control. I can find it and
set the standard properties like Top and Left, but since a standard object
does not have the property of "ControlRes" I have no access to it. I have to
add the custom control dynamically due to not knowing how many I need.
Someone please help.
 
M

Matt Berther

Hello Bob,

You'll need to do a cast to get access to that... or even easier, do something
like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

This assumes that MyCustomControl is the type of custom object that contains
the ControlRes property.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

Since in the Controls collection there would be other controls which are not
of type MyCustomControl (e.g. Buttons, Labels, etc) this loop will fail for
sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";

}

Also keep in mind that a control might be a child of another contorl in this
case it won't be found in the form Control collection.
 
G

Guest

ok Guy's I am now having fits of Joy because I am so close. MY problem is
now converting the C# syntax to VB. I have made several attempt and failed.
I understand the concept but do not quite have it. This line is confusing
me

MyCustomControl myCtrl = ctrl as MyCustomControl

any help would allow me to sleep.


Stoitcho Goutsev (100) said:
Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

Since in the Controls collection there would be other controls which are not
of type MyCustomControl (e.g. Buttons, Labels, etc) this loop will fail for
sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";

}

Also keep in mind that a control might be a child of another contorl in this
case it won't be found in the form Control collection.
 
G

Guest

OK I am having Fits of Joy because I am close. I am having problems
converting the C# syntax to VB..but I understand the concept This line is
confusing me:

MyCustomControl myCtrl = ctrl as MyCustomControl;

any help would allow me to sleep.. thanks



Stoitcho Goutsev (100) said:
Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

Since in the Controls collection there would be other controls which are not
of type MyCustomControl (e.g. Buttons, Labels, etc) this loop will fail for
sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";

}

Also keep in mind that a control might be a child of another contorl in this
case it won't be found in the form Control collection.
 
M

Matt Berther

Hello Stoitcho Goutsev (100) [C# MVP],

The loop that I mentioned is exactly the same as yours... Non-MyCustomControl
objects would never trigger the loop.

--
Matt Berther
http://www.mattberther.com
Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
Since in the Controls collection there would be other controls which
are not of type MyCustomControl (e.g. Buttons, Labels, etc) this loop
will fail for sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";
}

Also keep in mind that a control might be a child of another contorl
in this case it won't be found in the form Control collection.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
Hello Bob,

You'll need to do a cast to get access to that... or even easier, do
something like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
This assumes that MyCustomControl is the type of custom object that
contains the ControlRes property.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Matt,

Just one non-MyCustomControl and the loop will throw an exception
InvalidCastException.

You can try it.


--
Stoitcho Goutsev (100) [C# MVP]

Matt Berther said:
Hello Stoitcho Goutsev (100) [C# MVP],

The loop that I mentioned is exactly the same as yours...
Non-MyCustomControl objects would never trigger the loop.

--
Matt Berther
http://www.mattberther.com
Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
Since in the Controls collection there would be other controls which
are not of type MyCustomControl (e.g. Buttons, Labels, etc) this loop
will fail for sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";
}

Also keep in mind that a control might be a child of another contorl
in this case it won't be found in the form Control collection.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
Hello Bob,

You'll need to do a cast to get access to that... or even easier, do
something like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
This assumes that MyCustomControl is the type of custom object that
contains the ControlRes property.

--
Matt Berther
http://www.mattberther.com
I have been searching the www for 3 hours. I have a Custom Object
with a Public Property of "ControlRes" I have a win form that I
dynamically spray this control onto with the name of "Image1" at
run time I can not access the Image1.ControlRes property. I have
tried using for each Control in Countrols.count and looking for the
name of the control. I can find it and set the standard properties
like Top and Left, but since a standard object does not have the
property of "ControlRes" I have no access to it. I have to add the
custom control dynamically due to not knowing how many I need.
Someone please help.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Bob,

*as* operator in c# is cast operator. the difference between casting
Foo f = (Foo)obj;
and
Foo f = obj as Foo;

is that if obj cannot be cast to Foo the first will throw an exception where
the second will set 'f' to null.

Obviously *as* opertator cannot be used with value types.


--
HTH
Stoitcho Goutsev (100) [C# MVP]
Bob said:
ok Guy's I am now having fits of Joy because I am so close. MY problem is
now converting the C# syntax to VB. I have made several attempt and
failed.
I understand the concept but do not quite have it. This line is
confusing
me

MyCustomControl myCtrl = ctrl as MyCustomControl

any help would allow me to sleep.


Stoitcho Goutsev (100) said:
Hi,
foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

Since in the Controls collection there would be other controls which are
not
of type MyCustomControl (e.g. Buttons, Labels, etc) this loop will fail
for
sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";

}

Also keep in mind that a control might be a child of another contorl in
this
case it won't be found in the form Control collection.



--
HTH
Stoitcho Goutsev (100) [C# MVP]

Matt Berther said:
Hello Bob,

You'll need to do a cast to get access to that... or even easier, do
something like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}

This assumes that MyCustomControl is the type of custom object that
contains the ControlRes property.

--
Matt Berther
http://www.mattberther.com

I have been searching the www for 3 hours. I have a Custom Object
with a Public Property of "ControlRes" I have a win form that I
dynamically spray this control onto with the name of "Image1" at run
time I can not access the Image1.ControlRes property. I have tried
using for each Control in Countrols.count and looking for the name of
the control. I can find it and set the standard properties like Top
and Left, but since a standard object does not have the property of
"ControlRes" I have no access to it. I have to add the custom control
dynamically due to not knowing how many I need. Someone please help.
 
M

Matt Berther

Hello Stoitcho Goutsev (100) [C# MVP],

Hrmm... I did not know that.

Thanks!

--
Matt Berther
http://www.mattberther.com
Matt,

Just one non-MyCustomControl and the loop will throw an exception
InvalidCastException.

You can try it.

Hello Stoitcho Goutsev (100) [C# MVP],

The loop that I mentioned is exactly the same as yours...
Non-MyCustomControl objects would never trigger the loop.

--
Matt Berther
http://www.mattberther.com
Hi,

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
Since in the Controls collection there would be other controls which
are not of type MyCustomControl (e.g. Buttons, Labels, etc) this
loop will fail for sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";
}
Also keep in mind that a control might be a child of another contorl
in this case it won't be found in the form Control collection.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
Hello Bob,

You'll need to do a cast to get access to that... or even easier,
do something like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
This assumes that MyCustomControl is the type of custom object that
contains the ControlRes property.
--
Matt Berther
http://www.mattberther.com
I have been searching the www for 3 hours. I have a Custom
Object with a Public Property of "ControlRes" I have a win form
that I dynamically spray this control onto with the name of
"Image1" at run time I can not access the Image1.ControlRes
property. I have tried using for each Control in Countrols.count
and looking for the name of the control. I can find it and set
the standard properties like Top and Left, but since a standard
object does not have the property of "ControlRes" I have no access
to it. I have to add the custom control dynamically due to not
knowing how many I need. Someone please help.
 
G

Guest

Hi Matt

In VB.NET, there is no direct equivalent to the C# as operator - but you can
use the TypeOf operator and CType to achieve the same effect.

So your loop could look like this:

For Each c As Control In Me.Controls
If TypeOf c Is MyCustomControl Then
Dim mc As MyCustomControl = CType(c, MyCustomControl)

End If

Next


HTH

Nigel Armstrong
Matt Berther said:
Hello Stoitcho Goutsev (100) [C# MVP],

Hrmm... I did not know that.

Thanks!

--
Matt Berther
http://www.mattberther.com
Matt,

Just one non-MyCustomControl and the loop will throw an exception
InvalidCastException.

You can try it.

Hello Stoitcho Goutsev (100) [C# MVP],

The loop that I mentioned is exactly the same as yours...
Non-MyCustomControl objects would never trigger the loop.

--
Matt Berther
http://www.mattberther.com
Hi,

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
Since in the Controls collection there would be other controls which
are not of type MyCustomControl (e.g. Buttons, Labels, etc) this
loop will fail for sure.

To correct the loop we need to add type checking

foreach(Control ctrl in this.Controls)
{
MyCustomControl myCtrl = ctrl as MyCustomControl;
if(myCtrl != null)
myCtrl.ControlRes = "whatever";
}
Also keep in mind that a control might be a child of another contorl
in this case it won't be found in the form Control collection.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
Hello Bob,

You'll need to do a cast to get access to that... or even easier,
do something like this:

foreach (MyCustomControl control in Controls)
{
control.ControlRes = "whatever";
}
This assumes that MyCustomControl is the type of custom object that
contains the ControlRes property.
--
Matt Berther
http://www.mattberther.com
I have been searching the www for 3 hours. I have a Custom
Object with a Public Property of "ControlRes" I have a win form
that I dynamically spray this control onto with the name of
"Image1" at run time I can not access the Image1.ControlRes
property. I have tried using for each Control in Countrols.count
and looking for the name of the control. I can find it and set
the standard properties like Top and Left, but since a standard
object does not have the property of "ControlRes" I have no access
to it. I have to add the custom control dynamically due to not
knowing how many I need. Someone please help.
 

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