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

property with defalut value will not be called in auto-generated code, and how to make a property only visible at design time?

P: n/a
Hi,

What is the best practise to sove this problem:

property with defalut value will not be called in auto-generated code

?

for example

there is a property in a user control

private bool normalMode = false;
public bool NormalMode
{
get
{
return this.normalMode ;
}
set
{
SetAsNormal(value);
this.normalMode = value;
}

If I put this user control in a form, in forms InitializeComponent(), there
is no code like
userControl1.NormalMode = false;

so line like SetAsNormal(value); will not be called.

What is the best way to solve this? And how to make a property only visible
at design time?

Thanks a lot!
Ryan Liu
Aug 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Re the default value, I would simply ensure that this code gets called
in the ctor.

Re the design time... pass.

Marc

Aug 27 '06 #2

P: n/a
Ryan,

The only way that this will get called is if you have a value that is
different from the default value. You need to do this:

private bool normalMode = false;

[DefaultValue(false)];
public bool NormalMode
{
get
{
return this.normalMode ;
}
set
{
SetAsNormal(value);
this.normalMode = value;
}
}

Then, the designer will set the value for your component in
InitializeComponent method when you set a value to something OTHER than the
default value. If it is NOT the default value, then it ^will^ not and
^should^ not be set.

If you have a need for SetAsNormal(value) to be called, then in your
constructor, you should call SetAsNormal(normalMode).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ryan Liu" <ad********@online.sh.cnwrote in message
news:ec**************@TK2MSFTNGP03.phx.gbl...
Hi,

What is the best practise to sove this problem:

property with defalut value will not be called in auto-generated code

?

for example

there is a property in a user control

private bool normalMode = false;
public bool NormalMode
{
get
{
return this.normalMode ;
}
set
{
SetAsNormal(value);
this.normalMode = value;
}

If I put this user control in a form, in forms InitializeComponent(),
there
is no code like
userControl1.NormalMode = false;

so line like SetAsNormal(value); will not be called.

What is the best way to solve this? And how to make a property only
visible
at design time?

Thanks a lot!
Ryan Liu


Aug 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.