19

I have a class (control), implementing ICustomTypeDescriptor, which is used both at design-time and run-time by PropertyGrid for customization. I need to expose different properties at design-time (standard controls properties like width, height and so on) and at run-time, when PropertyGrid is used in my program to change other properties of that control.

My code is like:

class MyControl : UserControl, ICustomTypeDescriptor
{
    //Some code..

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }

    public PropertyDescriptorCollection GetProperties()
    {
        //I need to do something like this:
        if (designTime)
        { //Expose standart controls properties
            return TypeDescriptor.GetProperties(this, true);
        }
        else
        {
            //Forming a custom property descriptor collection
            PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null);
            //Some code..
            return pdc;
        }
    }
}

Is there an analog for a design-time flag in C#? Is it maybe better to use conditional compilation?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Tadeusz
  • 6,045
  • 8
  • 33
  • 49

3 Answers3

13

Check if DesignMode is true or false. It's a property that belongs to the control base class.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
DeveloperX
  • 4,568
  • 15
  • 22
  • 2
    Actually, it belongs to the `System.ComponentModel.Component` base class. – tafa Nov 16 '11 at 07:14
  • `'Component.DesignMode' is inaccessible due to its protection level` How do I get around this error ? – GuidoG Sep 02 '21 at 12:37
8

The flag should be DesignMode. Hence your code should look as follows

public PropertyDescriptorCollection GetProperties()
{
   //I need to do something like this:
   if (this.DesignMode)
   { //Expose standart controls properties
       return TypeDescriptor.GetProperties(this, true);
   }
   else
   {   //Forming a custom property descriptor collection
       PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null);
       //Some code..
       return pdc;      
   }
}

Here is the according MSDN doc.

Juri
  • 31,426
  • 18
  • 100
  • 133
3

Use the DesignMode property of the base. This will tell you about the mode.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kangkan
  • 14,505
  • 9
  • 68
  • 109