473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Propertygrid and objects within objects

Hi
I have an object whose properties are exposed in a property grid using the
usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of
the first object (objMain), thereby getting all of the properties for the
objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would
somehow reconcile itself. Is there any way that something like this could
work?

Thanks for any help anyone can provide.

Marek
Nov 16 '05 #1
3 2255
Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is MainMemberType))
{
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType ),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

Just as a note, if you want this to apply to the property of objMain, rather
than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.

--
Sean Hederman

http://codingsanity.blogspot.com

"Marek Wasilewski" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
Hi
I have an object whose properties are exposed in a property grid using the
usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of
the first object (objMain), thereby getting all of the properties for the
objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would
somehow reconcile itself. Is there any way that something like this could
work?

Thanks for any help anyone can provide.

Marek

Nov 16 '05 #2
Hi Sean
First of all that did the trick - thank you.

But, as with most of these things, it has exposed another question.
objMainMember then has another property objIProperty, which is a class that
implements an iterface (created using CreateInstance on the fly). I now
need to expose this object to the property grid. I repeated the procedure
you described on my derived class, but the only property I get is the name
of objIProperty, with an associated dropdown which only contains (none).

The class in question looks something like this:
[TypeConverter(typeof(MyRealObjConverter))]
public class MyRealObj : IBaseInterface

I implement the methods required by the interface and then add some more
properties, which are the ones I really need to expose to the property grid.

Is this something that can be done?

Best regards,

Marek
"Sean Hederman" <us*******@doesntexist.com> wrote in message
news:cr**********@ctb-nnrp2.saix.net...
Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is MainMemberType)) {
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType ),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
return true;
}

Just as a note, if you want this to apply to the property of objMain, rather than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.

--
Sean Hederman

http://codingsanity.blogspot.com

"Marek Wasilewski" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
Hi
I have an object whose properties are exposed in a property grid using the usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of the first object (objMain), thereby getting all of the properties for the objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would somehow reconcile itself. Is there any way that something like this could work?

Thanks for any help anyone can provide.

Marek


Nov 16 '05 #3
Hmm,

I'd say that you probably have to apply the TypeConverter to either the
interface or the property. I haven't done this before (TypeConverters on
interfaces), so I might be very wrong here.

Regards

--
Sean Hederman

http://codingsanity.blogspot.com

"Marek Wasilewski" <ma**************@dnv.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
Hi Sean
First of all that did the trick - thank you.

But, as with most of these things, it has exposed another question.
objMainMember then has another property objIProperty, which is a class
that
implements an iterface (created using CreateInstance on the fly). I now
need to expose this object to the property grid. I repeated the procedure
you described on my derived class, but the only property I get is the name
of objIProperty, with an associated dropdown which only contains (none).

The class in question looks something like this:
[TypeConverter(typeof(MyRealObjConverter))]
public class MyRealObj : IBaseInterface

I implement the methods required by the interface and then add some more
properties, which are the ones I really need to expose to the property
grid.

Is this something that can be done?

Best regards,

Marek
"Sean Hederman" <us*******@doesntexist.com> wrote in message
news:cr**********@ctb-nnrp2.saix.net...
Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context,

CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is

MainMemberType))
{
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType ),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext

context)
{
return true;
}

Just as a note, if you want this to apply to the property of objMain,

rather
than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.

--
Sean Hederman

http://codingsanity.blogspot.com

"Marek Wasilewski" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
> Hi
> I have an object whose properties are exposed in a property grid using the > usual method:
>
> _propertyGrid.SelectedObject = objMain;
>
> However, what I want to do is for the property grid to also display the
> properties of an object (objMainMember) which is defined as a property of > the first object (objMain), thereby getting all of the properties for the > objMain and objMainMember objects in one SelectedObject assignment.
>
> I was hoping that by exposing the member object that the whole thing would > somehow reconcile itself. Is there any way that something like this could > work?
>
> Thanks for any help anyone can provide.
>
> Marek
>
>



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
0
by: newbie | last post by:
Hello All, I am trying to develop an application to edit various 'entities' in a database. Entities include User, Group, Department, Test, System, approximately 20 - 30 class. Several classes...
0
by: Steve Randall | last post by:
I have a class that contains a member variable that is a hashtable. This hashtable holds a number of other class objects and it these other class objects that I want to be able to manage using the...
1
by: ANDRES BECERRA | last post by:
Herfried K. Wagner was kind enough to point me to the PropertyGrid control http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformspropertygridclasstopic.asp I have found a few...
3
by: Lance | last post by:
I have a situation where I want to show a property when one object is selected in a PropertyGrid, but I want to hide that property when more than one object is selected. For example, lets say I have...
6
by: Steve Teeples | last post by:
Can someone show me an example of how to place a "CheckedListBox" property within a PropertyGrid? -- ----------- Thanks, Steve
4
by: Steve | last post by:
I am using a property grid to display a classes properties, all is OK for single value properties but i need to display a list of strings for the user to select from and update the property value...
3
by: =?Utf-8?B?U3RldmVU?= | last post by:
Is it possible to hide a row within a PropertyGrid based upon the boolean value of another row within the PropertyGrid? I am using VS2005 with .NET Frameworks 2.0. -- ----------- Thanks,...
0
by: Sparky74 | last post by:
I have a number of Int32 properties that I want to display in a PropertyGrid control. The problem I have is that I want to be able to bypass the framework's default validation. If you have an Int32...
0
by: ullner | last post by:
I need to add two objects to a PropertyGrid, but when using SelectedObjects, the propertygrid only display those attributes that are common between the two objects. I've looked at CodeProject's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.