Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I have many panels and textboxes in my windows form application and a PropertyGrid.
When the user clicks on any panel or textbox the PropertyGrid dispays the properties.
 
However there are things that I don't want the user change. I want the user just modify things like size,location,text. There are many things in PropertyGrid that I don't want to show. How can I remove them? How can I choose what to view.
Posted 5-Jan-13 10:52am
Edited 5-Jan-13 10:53am
v2
Comments
BillWoodruff at 6-Jan-13 19:51pm
   

Hi Missak, Your question has inspired me to go ahead and implement a "relatively lightweight" solution for achieving selective display of editable Properties in a PropertyGrid without getting into the hairy depths of custom TypeDescriptors, without creating a Control that inherits from PropertyGrid, etc.
 
Displaying the Properties: Size, Location, Color, Font, and Text: is now working. And, properties can be displayed as read-only.
 
I am going to write this up for CP (not sure yet if it will be an article or tip/trick); do you have Visual Studio with .NET 4.0 available ? If so, I'll post a link to what's in progress for you to take a look at.
 
cheers, Bill
missak boyajian at 7-Jan-13 6:13am
   
Yes I have Visual Studio 2012 with .NET 4.0 available. Thanks for the help.
BillWoodruff at 7-Jan-13 8:05am
   
Hi, Missak, After some reflection on this: I have decided not to share the "proof of concept" code until it is complete, thoroughly commented, and tested.
 
I am concerned about putting out code that I am not available to answer questions about, or support, and improve. The article or tip/trick may be "up" in a week, or ten days.
 
And, also, I think I "owe it" to CP to review the many articles on PropertyGrid already posted here, to make sure I am not duplicating someone's earlier work.
 
However, you can start, if you are ready for it, making progress towards your own "lightweight" PropertyGrid solution by considering the fact you can create a Public Class with Public Properties, and set an instance of that Class to be the PropertyGrid.SelectedObject.
 
So, for every Control where you want the PropertyGrid to show Size, Location, Color, etc., you construct an instance of your "mirror Class," which has properties that duplicate the actual Control's Properties, and where the value of each Property has the current value of the same Property in the Control.
 
Then, when that Control is selected, you can set the "mirror Class" instance to be the SelectedObject of the PropertyGrid.
 
Then, you can use the PropertyValueChanged EventHandler of the PropertyGrid to detect a change to the value in the Property in the "mirror class," and update the actual Control the instance mirrors.
 
Work to date has convinced me this strategy is sound, and, for simpler tasks, like yours, will be much easier to use than dealing with the real intricacies of modifying the PropertyGrid at run-time to hide certain Properties, and only expose ones you have selected.
 
yrs, Bill
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have a look at ITypedList Interface[^] and ICustomTypeDescriptor Interface[^]
 
You can also look at: Using PropertyGrid to Display and Edit Dynamic Objects[^]
 
Best regards
Espen Harlinn
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 6-Jan-13 1:33am
   
Right, a 5. but I must note that the customization if Property Grid is a pretty difficult stuff. After I got it to work, I concluded that the root problem is very weak ComponentModel architecture. In part, this is related to more fundamental problems, such as lack of metaclasses and limitations of .NET attributes.
 
Interestingly: .NET has not many weaknesses, but it looks like all of those weaknesses were "reserved" to make Property Grid customization harder.. :-)
 
—SA
Espen Harlinn at 6-Jan-13 11:00am
   
Thank you, Sergey :-D
Mika Wendelius at 6-Jan-13 11:00am
   
Good references!
Espen Harlinn at 6-Jan-13 11:00am
   
Thank you, Mika :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

... edit #2 ...
CodeProject has many articles involving modifying the PropertyGrid: [^]
... end edit #2 ...
 
... edit #1 ...
In addition to the CP article linked to in Espen's comment above, you may find value in the CP article: "Making a PropertyGrid ReadOnly," by Rajeev511, 2010: [^]. Using the techniques in that article, it's not clear, to me, if you can make specific properties in the PropertyGrid readonly, and I have posted a question about that in the article's comments' section.
... end edit #1 ...

 
While I will share here some ideas, and code, that will kind of get you started on restricting which elements appear in a PropertyGrid, if you are only wanting to allow the end-user to edit just three Properties ('Location, 'Size, 'Text), I think you might consider just writing your own simple pop-up UserControl.
 
However, it is "cool" to see some visual aspect of a Control change when you change the value in a PropertyGrid, and hit return !
 
However, that could be simulated in a pop-up UserControl, also. mmmm ... maybe I should write a Tip/Trick on doing that ? Smile | :)
 
My comments are based on this 2006 CodeProject article, "Filtering properties in a PropertyGrid," by "bsargos" [^].
 
There is a fairly easy way to restrict what is shown in a PropertyGrid by certain CategoryAttributes, like "Layout," "Appearance," etc. You can find a current list (for .NET 4.0) of those attributes here [^].
 
But, restricting what is shown to "Layout," for example, while it will expose Location, and Size, Properties: will also expose many other Properties that you probably want to hide.
 
Here's some working code I wrote recently based on the CP article cited, that demonstrates using a CategoryAttribute restriction:
// be sure you have this reference !
using System.ComponentModel;
//
private Attribute fltrAttributeLayout = new CategoryAttribute("Layout");
 
private AttributeCollection attribCollection;
 
private void Form1_Load(object sender, EventArgs e)
{
    attribCollection = new AttributeCollection(new Attribute[]{fltrAttributeLayout});
 
    // note: once you've done this the PropertyGrid
    // is only going to show Layout Properties for any Control
    // set as its SelectedObject
    propertyGrid1.BrowsableAttributes = attribCollection;
}
 
// set any Control's Click Event to this Click_EventHandler
// for any Control where you want to activate the PropertyGrid
// with the Control's Layout Properties exposed
private void ShowPropertyGrid_Click(object sender, EventArgs e)
{
    // note: no need to cast to Control here
    propertyGrid1.SelectedObject = sender;
    propertyGrid1.Focus();
}
I hope these remarks, and this code, will be helpful to get you started. I have just begun to look into the more complex method of "fine-grained" editing which PropertyGrid elements appear, described in that 2006 CP article, and I'm researching the topic actively on both CP, and StackOverFlow. Would be good to find a simpler method. If I find one, I'll post it here.
 
Hope someone comes along with a full answer Smile | :)
  Permalink  
v4
Comments
missak boyajian at 6-Jan-13 6:11am
   
Thanks for the help. I also wanted to show elements like font and color and I added Appearance it worked. But in layout for example 3/4 items I don't need to show. If you find a solution how to remove elements from categories like layout , appearance... please post it.
Espen Harlinn at 6-Jan-13 6:37am
   
Nice reply :-D
 
I usually don't use Microsofts PropertyGrid, or most of the other controls provided by MS for forms development. DevExpress provides the PropertyGridControl: http://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraVerticalGridPropertyGridControltopic
which has a CustomPropertyDescriptors event: http://documentation.devexpress.com/#WindowsForms/DevExpressXtraVerticalGridPropertyGridControl_CustomPropertyDescriptorstopic
BillWoodruff at 6-Jan-13 20:04pm
   
Thanks, Espen, a commendation from someone with your level of technical knowledge, skill, and experience, really does mean something to me.
 
yrs, Bill
missak boyajian at 7-Jan-13 6:21am
   
I got confused what to use. I couldn't understand how to use DevExpress can you please show me how to use it in this case.
BillWoodruff at 7-Jan-13 6:51am
   
fyi: DevExpress is a company which sells developer components/controls, and tools, for .NET. Their PropertyGrid is part of a suite of WinForms custom Controls that, last time I looked, cost about US $900 per year to subscribe to. I am sure they do have a free trial :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CHill60 360
1 OriginalGriff 343
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Sergey Alexandrovich Kryukov 128


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid