Re: usercontrol with readonly property that can be set at design time
- From: mmcd79@xxxxxxxxxxxxxxx
- Date: 10 Jul 2006 18:35:51 -0700
Thank you both for responding :)
I did not think this was going to be such a difficult thing to do haha.
Maybe I'm just going about it the wrong way. I'm not the most
intuitive/knowledgable coder. This is just hobby for me.
I'm essentially writing a form of a wizard. I have a navigation panel
that currently contains several labels that have click events to
control which wizard page I'm on. All of my content pages are custom
usercontrols (which make no difference here).
Ultimately I'm trying to find out which label was clicked and show the
appropriate content for each label. My previous thought was to attach
a page property for each label (which in turn makes this a custom label
with this additional property and as such the purpose behind my entire
question). Any additional customlabels added (in the future) could
just have the next page value set (by developer) and then tied to
corresponding "content page".
I don't want to do any sort of case statement based on the control
name/text as this leaves no room for growth. Any developer picking
this up after me may modify the names/text of these labels to something
more meaningful so I want the properties to at least be a little more
meaningful in terms of their purpose and lenient in terms of these
changes.
I could just do this with a normal property, but I didn't want someone
changing the page values of the controls programmatically after they
have first been set. Just trying to figure out the "best" way to do
this, not some ugly hack if at all possible.
I appreciate the assistance given thus far and look forward to more
suggestions.
Stoitcho Goutsev (100) wrote:
Hi,
No attibutes will help you with that. DesignOnly attruibute won't help
because as the name suggests the values for such a property are design time
only - no code is generated in the form or user control to restore set up
such properties at runtime. The values are stored in the resource file and
are use at design time only.
If you declare the property as readonly or use readonly field it won't do
either. Don't forget that what ever you set in design time needs to be
applied in runtime and this is done in the IntializeComponent method. From
the CLR perspective this is just a method, so it has no power to set
non-writable or non-public properties and fields.
I see two solutions:
1. (that I'd suggest) is to use some variation on the Joris code.
I say variation becuse this code has one big flaw - when you run your
applicaiton the InitializeComponent will try to set the property and at this
moment DesignTime will return false and the control will throw an
exception. You should probably have some special value for this property
that will tell you whether the property is already initialized or not and
will allow to be initialized only once.
2. If you have a designer attached to your control you can shadow your
readonly property with read/write one, thus allowing users to set its value.
Then you need to supply your own CodeDomeSerializer that will generate
appropriate code to set somehow that property. Of course you need to have
some backdoor open for setting this proeprty such as an undocumented method.
There is something that bothers me though. You say that this is going to be
a static property. If it is exposed on the level of the control there might
be 2 or even more instances of your control on the form. Each of these
controls will try to set the static property. If using the first solution
you are going to get an exception as soon as the second control tries to set
it.
--
Stoitcho Goutsev (100)
"Joris Zwaenepoel" <JorisZwaenepoel@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:266AC829-3853-4C3B-8BF7-B3CE5113C534@xxxxxxxxxxxxxxxx
Not the most elegant solution, but I guess you could throw an exception if
the property is set in runtime (code not written in IDE, so not tested,
and
could have syntax errors).
Public Property MyProp As Integer
Get
Return _myProp
End Get
Set(ByVal value As Integer)
If Me.DesignTime Then
_myPorp = value
Else
Throw New ApplicationException("MyPorp is readonly in
runtime.")
End If
End Set
End Property
Joris
"mmcd79@xxxxxxxxxxxxxxx" wrote:
Can someone tell me if it's possible to create a property for a
usercontrol, that is essentially readonly, but able to be set via the
designer before runtime?
I have a property for a control that I want to set essentially one
time. This property is basically a static property that can change at
the developer's wish, but only one time. I need to be able to
reference the property later.
I've tried setting the property to readonly, but that hides it from the
designer properties window. I've tried various attributes to no avail
(i.e. DesignOnly(true)). When I do this, I cannot set the property at
design time(the value doesn't hold). But in later code I can change
that property without fail.
I'm confused. Please help.
.
- Follow-Ups:
- References:
- Prev by Date: Re: Capturing mouse event at higher level in containment hierarchy
- Next by Date: Re: usercontrol with readonly property that can be set at design time
- Previous by thread: usercontrol with readonly property that can be set at design time
- Next by thread: Re: usercontrol with readonly property that can be set at design time
- Index(es):
Relevant Pages
|