PC Review


Reply
Thread Tools Rate Thread

Control Property

 
 
Merlin
Guest
Posts: n/a
 
      10th Oct 2003
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.


 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      10th Oct 2003
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



"Merlin" <(E-Mail Removed)> wrote in message
news:bm5tiu$lpf$(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Rainman
Guest
Posts: n/a
 
      10th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:bm5tiu$lpf$(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Merlin
Guest
Posts: n/a
 
      10th Oct 2003
Thanks Bob

That did the trick!

Merlin
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%(E-Mail Removed)...
> 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
>
>
>
> "Merlin" <(E-Mail Removed)> wrote in message
> news:bm5tiu$lpf$(E-Mail Removed)...
> > 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.
> >
> >

>
>



 
Reply With Quote
 
Phill. W
Guest
Posts: n/a
 
      10th Oct 2003
"Merlin" <(E-Mail Removed)> wrote in message
news:bm5tiu$lpf$(E-Mail Removed)...
.. . .
> 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.


 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      11th Oct 2003
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:bm672n$9om$(E-Mail Removed)...
> "Merlin" <(E-Mail Removed)> wrote in message
> news:bm5tiu$lpf$(E-Mail Removed)...
> . . .
> > 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exposing control property as list of items on property sheet... Charlie@CBFC Microsoft Dot NET Framework Forms 0 17th Nov 2006 05:31 PM
can a custom control have an image property that can be set in design time from the property browser? news.austin.rr.com Microsoft Dot NET Compact Framework 3 3rd Mar 2005 02:08 AM
Custom Control Property HelpText Property =?Utf-8?B?R3JleUFsaWVuMDA3?= Microsoft Dot NET Framework Forms 1 20th Jan 2005 06:17 AM
Adding code property to control property window NuBBeR Microsoft C# .NET 1 7th Oct 2004 05:15 PM
How to throw and catch an exception in the property set when property is bound to a wiform control Keith Roe Microsoft Dot NET Framework 2 4th Dec 2003 05:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:50 AM.