Control Property

  • Thread starter Thread starter Merlin
  • Start date Start date
M

Merlin

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.
 
Use reflection to examine the object and get it's properties. Then
manipulate the property via the property decriptor.

dim pdc as
PropertyDescriptorCollection=TypeDescriptor.GetProperties(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
 
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
 
Thanks Bob

That did the trick!

Merlin
Bob Powell said:
Use reflection to examine the object and get it's properties. Then
manipulate the property via the property decriptor.

dim pdc as
PropertyDescriptorCollection=TypeDescriptor.GetProperties(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



changes
 
.. . .
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.
 
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.
 
Back
Top