Problem with the filtering of a property grid

A

alexcsharp2

Hello.

I created a designer in c#. Everything is working excepted the
property grid.

I want to show only the properties I added. So I created a class which
inherits of a button. And I added properties.

But I want to filter the base properties. So to do this, I inherit my
class from ICustomTypeDescriptor.

In the function getProperties, I filter. And I can show only my
properties. But on my host designer, the text of my button and the
position disappeared.

I mean when I add my custom button without filtering. I see it normal.
And if I filter, the text and position are removed. So it appears in
the left corner and I can't move it.

How can I fix the problem?

Thank you.
 
M

Marc Gravell

Can I double check? It sounds like you are hosting a PropertyGrid in
your app, and want to filter the propeties that appear inside your app,
while leaving the regular VS IDE "as is" - correct?

You can use the BrowsableAttributes property to specify a filter based
on attributes; by default, it uses:
new Attribute[] {BrowsableAttribute(true)}
Which hides properties with BrowsableAttribute(false) - but you can
specify your own. Alternatively, you can implement your own PropertyTab
(remove the default one, add yours instead) - and simply override
GetProperties in PropertyTab; that way you have complete control.

Marc
 
M

Marc Gravell

And here's an example of both approaches...

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

public class Foo
{
[MyAttribute]
public int BrowseNotSetWithCustom {get;set;}
[Browsable(true)]
public int BrowseTrueNoCustom {get;set;}
[Browsable(false), MyAttribute]
public int BrowseFalseWithCustom {get;set;}
}

class MyAttribute : Attribute {}
static class Program
{
static void Main()
{
// have 3 grids; one default, one using custom
// attributes, one using custom tab
Foo foo = new Foo();
Application.EnableVisualStyles();
PropertyGrid customGrid = new PropertyGrid
{
Left = 400,
Height = 400,
SelectedObject = foo,
BrowsableAttributes = new AttributeCollection()
};
customGrid.PropertyTabs.AddTabType(typeof(MyTab),
PropertyTabScope.Static);
Application.Run(
new Form {
Width = 800,
Height = 450,
Controls = {
new PropertyGrid {
Left = 0,
Height = 400,
SelectedObject = foo
},
new PropertyGrid {
Left = 200,
Height = 400,
BrowsableAttributes = new AttributeCollection(
new MyAttribute()),
SelectedObject = foo
},
customGrid
}
}
);
}
}


public class MyTab : PropertyTab
{
public MyTab() {

}
public override System.Drawing.Bitmap Bitmap
{
get
{
return SystemIcons.Shield.ToBitmap();
}
}
public override bool CanExtend(object extendee)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object component,
Attribute[] attributes)
{
return GetProperties(component, attributes);
}
public override PropertyDescriptorCollection GetProperties(object
component)
{
return GetProperties(component, null);
}
public override PropertyDescriptorCollection GetProperties(object
component, Attribute[] attributes)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(component, attributes);
List<PropertyDescriptor> list = new
List<PropertyDescriptor>(props.Count);
foreach (PropertyDescriptor prop in props)
{
if (prop.Name == "BrowseTrueNoCustom") continue; // skip
list.Add(prop);
}
return new PropertyDescriptorCollection(list.ToArray());
}
public override string TabName
{
get { return "Mwahahah"; }
}
}
 
A

alexcsharp2

Thank you marc for your sample. But I can't compile it. I have VS2005

Could you give me an example which works with VS2005. Moreover, I
didn't understand, why I need to use three propertygrid.

Could you please explain me? Thank you
 
A

alexcsharp2

Ok well I worked a lot and I fixed the problem. Thanks a lot Marc.

Just a question. When I want to add a component to my host designer, I
would like to show the picture of my component as visual. Do you how
can I do that?

Thanks
 
A

alexcsharp2

Ok well I worked on my problem. And it is fine. i fixed the problem.
Thanks a lot.

But i have an other question. Do you know how can I show the picture
of the component I want to add to my host designer when I chose it in
the toolbox?

Thanks alot for your help.
 
M

Marc Gravell

But I can't compile it. I have VS2005
From your later post, it sounds like this is sorted - but please let
me know if it isn't: I can port it to C# 2 easily enough (it just
takes more typing...)
I didn't understand, why I need to use three propertygrid.
The three grids was merely to show the difference between the standard
behaviour, the behavior with a different attribute filter, and the
behavior with a custom tab.

Marc
 
M

Marc Gravell

Just a question. When I want to add a component to my host designer, I
would like to show the picture of my component as visual. Do you how
can I do that?

Can you be more specific? Where do you want it to be graphic? For
example, you can specify a toolbox bitmap (from a file or a resource
inside the assembly) with ToolboxBitmapAttribute:
http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx

Or if you mean inside the property-grid (like how images, colours etc
hav a preview), this is the job of a UITypeEditor.

Marc
 
A

alexis.meilland

When I choose a component in my toolbox to add it to my designer, I
have a cross as cursor. And I would like to show the picture of the
component near the cursor as visual studio is doing.

Do you have an idea?

I have a last question. Sorry for my bad knowledge. I have firefox and
I installed piclens. The user interface is really cool and I would
like to create the same or closer. Do you think I can create it with
Visual Studio 2005 desktop application? Maybe I can use DirectX. Do
you have an other idea?

An other solution will be to se csharp 3.0. What could you advice me?

Thanks a lot. I really apreciate your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top