Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .Net 4.5 PropertyGrid: how to hide Properties

The problem is simple(and I hope that this have a simple solution!): I want to hide ( Browsable(false) ) the property "Element" (in my PropertyGrid object) when it's zero.

    public class Question
    {
       ...

      public int Element
      {
        get; set;
      }
    }
like image 275
Nicola Russo Avatar asked Sep 20 '13 21:09

Nicola Russo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

The easiest way to hide a property in PropertGrid and in a Custom Control for me is this:

public class Question
{
   ...
  
  [Browsable(false)]
  public int Element
  {
    get; set;
  }
}

To do it dynamically you can use this code, where Question is your class and your property is Element, so you can show or hide it without remove element from collection:

PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
PropertyDescriptor descriptor = propCollection["Element"];

BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
//Condition to Show or Hide set here:
isBrow.SetValue(attrib, true);
propertyGrid1.Refresh(); //Remember to refresh PropertyGrid to reflect your changes

So to refine the answer:

public class Question
{
   ...
   private int element;
   [Browsable(false)]
   public int Element
   {
      get { return element; }
      set { 
            element = value; 
            PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
            PropertyDescriptor descriptor = propCollection["Element"];
    
            BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            if(element==0)
            {
              isBrow.SetValue(attrib, false);
            }
            else
            {
              isBrow.SetValue(attrib, true);
            }
          }
   }
}
like image 147
Microlang Avatar answered Sep 18 '22 16:09

Microlang


What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

like this for example:

public Form1()
{
    InitializeComponent();

    DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(Question));

    Question q = new Question(); // initialize question the way you want    
    if (q.Element == 0)
    {
        dt.RemoveProperty("Element");
    }
    propertyGrid1.SelectedObject = dt.FromComponent(q);
}
like image 29
Simon Mourier Avatar answered Sep 19 '22 16:09

Simon Mourier