0

I use DefaultValue for a property so that it will be not generated in designer when I create a control which has this property in designer. However, with properties which have data type is not standard, example: Image, how to use DefaultValue. Thanks.

This is an example codes:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace DemoChart
{
  public class MyControl : Control
  {
    private Image m_Img = DemoChart.Properties.Resources.link;

    [DefaultValue("DemoChart.Properties.Resources.link")]
    public Image Img
    {
      get { return m_Img; }
      set { m_Img = value; }
    }

    public MyControl()
    {
    }
  }
}

Note: DemoChart.Properties.Resources.link is a Image resource which I add in project.

I create a form named Form1, drag a MyControl to this form and it name is myControl1. I open Form1.designer.cs to view generated codes, I still see myControl1's Img property. Why? I use DefaultValue but it doesn't have effect. Please help me. Thanks.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Leo Vo
  • 9,980
  • 9
  • 56
  • 78
  • http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value – Mitch Wheat May 17 '11 at 02:29
  • This does not solve my problem. Please read my question. DefaultValue(true) or DefaultValue(1) -> it is simple but with a Image object, how to use. Thanks. – Leo Vo May 17 '11 at 02:34

1 Answers1

3

If your property has a complex data type then you are supposed to use the ShouldSerialize and Reset methods.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83