HOWTO: Create a new Control and not have VS.NET set the Properties when Dragged onto Form

J

Joe Feser

I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required to tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting the name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
C

Chris Taylor

Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor
 
J

Joe Feser

I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Chris Taylor said:
Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


Joe Feser said:
I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required to tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting the name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
T

Tim Wilson [MVP]

The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one of the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
Joe Feser said:
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Chris Taylor said:
Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


Joe Feser said:
I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required
to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting
the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
J

Joe Feser

That does allow me to set a Color for my Default Value but neither solution
solves the original problem.

The original problem is I don't want VS.NET putting the property values in
the code when I drag a control onto the form.

I have like 20 lines of code being added every time I add this specific type
of control to the page, and I don't want any of that happening.

I just want the default stuff to appear like Name and Size.

Any ideas?

Joe

Tim Wilson said:
The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one of the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
Joe Feser said:
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Chris Taylor said:
Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
T

Tim Wilson [MVP]

If you want full control over how your component/control gets serialized
then you need to write a custom CodeDomSerializer.

--
Tim Wilson
Windows Embedded MVP

Joe Feser said:
That does allow me to set a Color for my Default Value but neither solution
solves the original problem.

The original problem is I don't want VS.NET putting the property values in
the code when I drag a control onto the form.

I have like 20 lines of code being added every time I add this specific type
of control to the page, and I don't want any of that happening.

I just want the default stuff to appear like Name and Size.

Any ideas?

Joe

Tim Wilson said:
The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one of the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
Joe Feser said:
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is
required
to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by
setting
the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
I

Iulian Ionescu

You can always do this:

protected virtual bool ShouldSerializeClickable()
{
return false;
}

The ShouldSerialize<propertyName> is discovered by the
designer through reflection. If this function returns
true it will add the code to the output. Usually you must
return true only if the value of the property is
different than the default value, like:

protected virtual bool ShouldSerializeClickable()
{
return this.Clickable != this.DefaultClickable;
}
protected virtual bool DefaultClickable
{
get {return false;}
}

and don't forget in the constructor to put:

this.Clickable = this.DefaultClickable;

Typically you should also add a ResetClickable() function
that restores the value to the default. If you do that
the users can right click on the property and choose
Reset without having to know what the default is...

hope this helps,
iulian
 
T

Tom

Why not just use the DesignerSerializationVisibility attribute?

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

Tom Clement
Apptero, Inc.

Joe Feser said:
That does allow me to set a Color for my Default Value but neither solution
solves the original problem.

The original problem is I don't want VS.NET putting the property values in
the code when I drag a control onto the form.

I have like 20 lines of code being added every time I add this specific type
of control to the page, and I don't want any of that happening.

I just want the default stuff to appear like Name and Size.

Any ideas?

Joe

Tim Wilson said:
The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one of the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
Joe Feser said:
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is
required
to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by
setting
the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
J

Joe Feser

This works great to keep vs.net from persisting the default properties when
you drag the control onto the form.

There is only one problem

If I then want to set the property manually, it will not save the value to
the code file.

Can the designer control have its value changed after it is dragged onto the
form?

Is there something I am doing wrong?

Joe

Tom said:
Why not just use the DesignerSerializationVisibility attribute?

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

Tom Clement
Apptero, Inc.

Joe Feser said:
That does allow me to set a Color for my Default Value but neither solution
solves the original problem.

The original problem is I don't want VS.NET putting the property values in
the code when I drag a control onto the form.

I have like 20 lines of code being added every time I add this specific type
of control to the page, and I don't want any of that happening.

I just want the default stuff to appear like Name and Size.

Any ideas?

Joe

Tim Wilson said:
The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one of the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because
there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required
to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting
the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 
T

Tom

Sorry I didn't read your original post carefully enough.

Yes there is a fairly easy way to have control over serialization. You
implement System.ComponentModel.ISupportInitialize on your control. For a
simple example of what the Forms designer does with this, create a new
Winforms application and drop a NumericUpDown control on it. You'll see how
it is invoked. Look up ISupportInitialize in the documentation for more
details.

Hope this helps.

Tom Clement
Apptero, Inc.

Joe Feser said:
This works great to keep vs.net from persisting the default properties when
you drag the control onto the form.

There is only one problem

If I then want to set the property manually, it will not save the value to
the code file.

Can the designer control have its value changed after it is dragged onto the
form?

Is there something I am doing wrong?

Joe

Tom said:
Why not just use the DesignerSerializationVisibility attribute?

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

Tom Clement
Apptero, Inc.

Joe Feser said:
That does allow me to set a Color for my Default Value but neither solution
solves the original problem.

The original problem is I don't want VS.NET putting the property
values
in
the code when I drag a control onto the form.

I have like 20 lines of code being added every time I add this
specific
type
of control to the page, and I don't want any of that happening.

I just want the default stuff to appear like Name and Size.

Any ideas?

Joe

The default value attribute works for basic types, and, with a little
"trickery", can be made to work with the Color struct as well:
DefaultValue(typeof(Color), "Red")

If you can't get the type to work, because it doesn't fit into one
of
the
constructors for the default value attribute, you need to create two
methods:
public Color MyColor
{
get {...}
set {...}
}
protected virtual bool ShouldSerializeMyColor()
{
return (MyColor != SystemColors.Highlight);
}
protected virtual void ResetMyColor ()
{
MyColor = SystemColors.Highlight;
}

--
Tim Wilson
Windows Embedded MVP
I already tried that and it does not work

Also you can't set a default for the System.Drawing.Color Type because
there
is no color defined as a constant.

This was one of the first things I tried.

Joe

Hi,

Add the DefaultValueAttribute, that should do the trick.

[
Category("Skinning"),
Description("Test description.")
DefaultValue( true )
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable

Hope this helps

Chris Taylor


I have a control that extends UserControl

I have added a few properties to this control

[
Category("Skinning"),
Description("Test description.")
]
public bool Clickable
{
get { return isClickable; }
set { isClickable = value; }
}// Clickable


When i drag the control onto a Form, VS.NET adds a line in teh
InitializeComponent method to set a default on that property

this.imageButton1.Clickable = true;

I am trying to figure out what attribute or other method is required
to
tell
VS.NET to not try to set the default value.

All I want is the control to act like a regular control, by setting
the
name
and size property, but to leave all other properties alone.

I want the item to still show up in the Properties box if possible.

Can this be done without using browsable=false?

Thank you

Joe Feser
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top