Accessing properties of multiple (different) controls

  • Thread starter Thread starter jralford
  • Start date Start date
J

jralford

Hi,

I have a main form with a number of controls on it. They are a range
of different customized controls.
There are some properties that belong to all of these controls, such
as XOffset, YOffset etc.
Currently, in order to get this value when for instance, the user
clicks on the control, I need to determine the type of control, then
reference it in the following manner:

int MyOffset;

String WhatType = sender.GetType();
if (WhatType == "WindowsApplication1.ucContType1) then
MyOffset = ((WindowsApplication1.ucConttype1)sender).XOffset
if (WhatType == "WindowsApplication1.ucContType2) then
MyOffset = ((WindowsApplication1.ucConttype2)sender).XOffset

As I am referencing a lot of properties on a lot of occasions, the
code starts to get a little unmanageable, even when putting these
lookups away in functions.

So, my question is, is there a way to access a property that I know
belongs to every one of these different types of controls, without
needing to find out the control type?

Thankyou.

James
 
Derive all of your controls from a common base that includes the XOffset
property.

HTH

DalePres
MCAD, MCDBA, MCSE


jralford said:
Hi,

I have a main form with a number of controls on it. They are a range
of different customized controls.
There are some properties that belong to all of these controls, such
as XOffset, YOffset etc.
Currently, in order to get this value when for instance, the user
clicks on the control, I need to determine the type of control, then
reference it in the following manner:

int MyOffset;

String WhatType = sender.GetType();
if (WhatType == "WindowsApplication1.ucContType1) then
MyOffset = ((WindowsApplication1.ucConttype1)sender).XOffset
if (WhatType == "WindowsApplication1.ucContType2) then
MyOffset = ((WindowsApplication1.ucConttype2)sender).XOffset

As I am referencing a lot of properties on a lot of occasions, the
code starts to get a little unmanageable, even when putting these
lookups away in functions.

So, my question is, is there a way to access a property that I know
belongs to every one of these different types of controls, without
needing to find out the control type?

Thankyou.

James
 
Thanks for the response.

Ok, so I can derive my controls from a common base.
Now in each control type I can see the property and get the values I
need.
However, it doesn't seem to solve the problem of having to get to the
property through each different type of control on the form and
therefore needing to go through the process I outlined in my original
post - to find out what type of control the clicked on object is, then
get the property with: ((controltye)sender).property

To clarify a little more if a user clicks on a control, I do not want
to know what type of control it is. I just want to access the
property.

Am I missing something?

thanks again.
 
You do not need to know the control's type because they all derive from a
base class X.
Each control you get is of type X as far as your code is concerned....

Eran Kampf
http://www.ekampf.com
http://www.ekampf.com/blog

jralford said:
Thanks for the response.

Ok, so I can derive my controls from a common base.
Now in each control type I can see the property and get the values I
need.
However, it doesn't seem to solve the problem of having to get to the
property through each different type of control on the form and
therefore needing to go through the process I outlined in my original
post - to find out what type of control the clicked on object is, then
get the property with: ((controltye)sender).property

To clarify a little more if a user clicks on a control, I do not want
to know what type of control it is. I just want to access the
property.

Am I missing something?

thanks again.
 
Back
Top