PropertyGrid sees only public properties

  • Thread starter Thread starter Koen
  • Start date Start date
K

Koen

The things you can do with a propertygrid are fantastic. Howeveer all
properties you want to appear in the grid need to be public and all
properties that you want to be editable need to have a set accessor.
How can I make my class expose the properties differently for a PropertyGrid
than for normal use in the code?

Some more details: I want to use the propertygrid to edit resources. I
already have a class that can read/write these resources but I do not want
to change these resources at run-time, so I don't want a 'set' of the
properties to be publicly visible (malicious programmers are always around
to change some common resource). However a property grid is ideal to edit
the class and hence the resources I could do a run-time check in the set of
the property to see if the object is writable, but it is so much cleaner
that a call that will go through set is trapped by the compiler (like when
no set accessor exists) .
I also want to expose some properties in their full complexity while in
run-time they are simply exposed like strings. So the type of the property
would change between runtime and peropertygrid

Koen.
 
I had a similar situation a while back, I was using a third-party class that
I wanted to allow the user access to, but the class exposed more than just
the properties I wanted exposed, and in some cases, not in the format I
wanted.

So I created a wrapper class that limited the access to just what I wanted.

In your case, you may be able to have the wrapper be a public class, but the
inside class be internal (or something).

That's the best I can suggest.
 
Or...

Make a public get property and an internal set property (with different
names unfortunately (but with C# that can be as simple as a different case)):

public int X { get { return x ; } }
internal int SetX { set { x = value ; } }

or make the field internal (yuck).

or a method:

public void SetX ( int X ) { x = X ; }

With different case (won't work with VB):

class MyObj
{
private int xval = 0 ;
....
public int Xval { get { return xval ; } }
internal int XVal { get { return Xval ; } set { xval = value ; } }
}
 
Or... a modification of my earlier suggestion of a wrapper class,make the
wrapped class _inside_ the wrapper class:

namespace AccessabilityTest
{
public class OuterClass
{
internal class InnerClass
{
private int xval = 0 ;

internal int Xval { get { return xval ; } set { xval = value ; } }
}

internal InnerClass inner = new InnerClass();

public int Xval { get { return inner.Xval ; } }
}

public class TesterClass
{
[System.STAThread]
static void Main(string[] args)
{
OuterClass outer = new OuterClass() ;

System.Console.WriteLine ( outer.Xval ) ;
System.Console.WriteLine ( outer.inner.Xval ) ;

// outer.Xval = 5 ; // compile-time error
outer.inner.Xval = 5 ;

System.Console.WriteLine ( outer.Xval ) ;
System.Console.WriteLine ( outer.inner.Xval ) ;

return ;
}
}
}
 
And what about:

public int X
{
get
{
return x ;
}

set
{
if ( DesignMode )
{
x = value ;
}
}
}
 
namespace AcTest
{
public class AcClass : System.ComponentModel.Component
{
private int x = 0 ;
private bool frozen = false ;

public AcClass()
{
}

public int
X
{
get
{
return ( this.x ) ;
}

set
{
if ( this.DesignMode || !this.frozen )
{
this.x = value ;
this.frozen = true ;
}
else
{
System.Windows.Forms.MessageBox.Show
(
"You may not change the value of X"
,
"Read-only"
,
System.Windows.Forms.MessageBoxButtons.OK
,
System.Windows.Forms.MessageBoxIcon.Hand
) ;
}
}
}
}
}
 
[System.ComponentModel.CategoryAttribute("Margins"),
System.ComponentModel.ReadOnlyAttribute(true),
System.ComponentModel.Description("Margin settings.")]
public Margins Margins
{
get
{
return ( this.margins ) ;
}

set
{
this.margins.Top = value.Top ;
this.margins.Bottom = value.Bottom ;
this.margins.Left = value.Left ;
this.margins.Right = value.Right ;
this.margins.Gutter = value.Gutter ;
this.margins.MirrorMargins = value.MirrorMargins ;
}
}
 
Could you mark the public/private declarations in a compiler directive and
compile the assemblies you use to add Items to the toolbox with the public
directive, and the release assemblies with the private directive. Not the
cleanest solution, but one that may work.

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Back
Top