Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i added propertyGrid in runtime for my label and i want just show "Text","ForeColor","Font" in propertyGrid
how can i do it?
Posted
Comments
Kornfeld Eliyahu Peter 4-Mar-15 12:24pm    
Do you mean ONLY those properties and not any other?
Avenger1 4-Mar-15 12:52pm    
i want just show those properties not other properties

All public properties of the SelectedObject will be displayed in the PropertyGrid by default. You can hide a property so that it is not displayed in the PropertyGrid control by decorating it with the BrowsableAttribute and setting the value to false.

So there is your problem, you are using a control made by others (Microsoft in this case) and you can not control what property is public and what not...
You have two options:
1. Use refection and decorate all the properties you want to hide with BrowsableAttribute=false[^] (dirty job)
2. Wrap the original control inside an other that will make public only the requested properties...Assign that object to PropertyGrid and use the wrapped original in anywhere else...
 
Share this answer
 
v2
There is a work-around for this that does not require reflection and munging the weirdness of the PropertyGrid. Try this:

1. create a UserControl, put a Button in it named 'button1, or whatever.

2. set the UserControl's AutoSize, SizeMode, Padding, Properties, etc., as you require.

3. set the Button's AutoSize and SizeMode properties as you require. Size and position the Button in the UserControl where you want it.

... think about the possible interactions here of changing, for example, the FontSize at run-time: if both the UserControl and the Button have their AutoSizeMode Properties set to 'GrowAndShrink, and their AutoSize Mode set to 'Auto: then the Size of both Button and UserControl will change ...

4. compile, and drag-drop instances of 'ButtonExLimited onto your Form as you require

5. example: when you want to edit the exposed Properties in a Property Grid:

propertyGrid1.SelectedObject = testButtonPropsExpose1.TheExposed;

6. example: when you to add an Event Handler to Button:

testButtonPropsExpose1.TheExposed.TheControl.Click += TheControl_Click;

7. Code for the UserControl:
C#
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class TestButtonPropsExpose : UserControl
    {
        public TestButtonPropsExpose()
        {
            InitializeComponent();
            TheExposed.TheControl = this.button1;
        }

        public TheExposedProps TheExposed = new TheExposedProps();
    }

    public class TheExposedProps
    {
        public Control TheControl;

        public string Text
        {
            get { return TheControl.Text; }
            set { TheControl.Text = value; }
        }
        public Color ForeColor
        {
            get { return TheControl.ForeColor; }
            set { TheControl.ForeColor = value; }
        }

        public new Font Font
        {
            get { return TheControl.Font; }
            set { TheControl.Font = value; }
        }
    }
}
Notes:

1. in production code I use a fancier version of this I intend to publish on CP
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900