By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,685 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,685 IT Pros & Developers. It's quick & easy.

Control Property

P: n/a
Hi,

I'm creating a function that looks at every control on a form and changes a
property if it exists. I've coded so far to find every control, including
nested - but how do I determin if a property exists first so that it can be
changed. e.g change the TEXT of every control (but not all controls may have
a property of TEXT).

Many Thanks.

Merlin.
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Use reflection to examine the object and get it's properties. Then
manipulate the property via the property decriptor.

dim pdc as
PropertyDescriptorCollection=TypeDescriptor.GetPro perties(myControl)
dim pd as PropertyDescriptor = pdc.Find("Location",false)
if not pd is nothing then
'do something to the property here...
end if
--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Merlin" <iM*****@hotmail.com> wrote in message
news:bm**********@sparta.btinternet.com...
Hi,

I'm creating a function that looks at every control on a form and changes a property if it exists. I've coded so far to find every control, including
nested - but how do I determin if a property exists first so that it can be changed. e.g change the TEXT of every control (but not all controls may have a property of TEXT).

Many Thanks.

Merlin.

Nov 20 '05 #2

P: n/a
try the following code samples

For Each c As Control In Me.Controls
If Not (c.GetType().GetProperty("YourAttribute") Is Nothing)
Then

' add your set property code here

End If
Next

Hope It Helps!

Microsoft .NET MVP
Shanghai China

--
Microsoft .NET MVP
"Merlin" <iM*****@hotmail.com> wrote in message
news:bm**********@sparta.btinternet.com...
Hi,

I'm creating a function that looks at every control on a form and changes a property if it exists. I've coded so far to find every control, including
nested - but how do I determin if a property exists first so that it can be changed. e.g change the TEXT of every control (but not all controls may have a property of TEXT).

Many Thanks.

Merlin.

Nov 20 '05 #3

P: n/a
Thanks Bob

That did the trick!

Merlin
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Use reflection to examine the object and get it's properties. Then
manipulate the property via the property decriptor.

dim pdc as
PropertyDescriptorCollection=TypeDescriptor.GetPro perties(myControl)
dim pd as PropertyDescriptor = pdc.Find("Location",false)
if not pd is nothing then
'do something to the property here...
end if
--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Merlin" <iM*****@hotmail.com> wrote in message
news:bm**********@sparta.btinternet.com...
Hi,

I'm creating a function that looks at every control on a form and changes
a
property if it exists. I've coded so far to find every control,

including nested - but how do I determin if a property exists first so that it can

be
changed. e.g change the TEXT of every control (but not all controls may

have
a property of TEXT).

Many Thanks.

Merlin.


Nov 20 '05 #4

P: n/a
"Merlin" <iM*****@hotmail.com> wrote in message
news:bm**********@sparta.btinternet.com...
.. . .
I'm creating a function that looks at every control on a form and
changes a property if it exists. .. . . how do I determin if a property exists first so that it can be
changed.


There's probably some clever way using Reflection, but why bother?
Try and set the property, Catch the Exception thrown if the property
doesn't exist and do precisely nothing about it, as in

For Each eControl as Object in Me.Controls
Try
eControl.Checked = True
Catch MissingMemberException
' Do Nothing
End Try
Next

Of course
HTH,
Phill W.
Nov 20 '05 #5

P: n/a
If he has Option Strict turned on (which is a very good idea), late binding
won't be allowed and your code won't work.
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:bm**********@yarrow.open.ac.uk...
"Merlin" <iM*****@hotmail.com> wrote in message
news:bm**********@sparta.btinternet.com...
. . .
I'm creating a function that looks at every control on a form and
changes a property if it exists.

. . .
how do I determin if a property exists first so that it can be
changed.


There's probably some clever way using Reflection, but why bother?
Try and set the property, Catch the Exception thrown if the property
doesn't exist and do precisely nothing about it, as in

For Each eControl as Object in Me.Controls
Try
eControl.Checked = True
Catch MissingMemberException
' Do Nothing
End Try
Next

Of course
HTH,
Phill W.

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.