
Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton
Greets everyone! I've been trying to solve this problem for about 3
hours now and I'm just coming up short and I'm keeping my fingers
crossed that a fresh set of eyes and a new perspective will help.
Here's the (very simplified) deal...
I have a Windows app with two forms and a Singleton. The Singleton has
one property. FormA holds the Singleton object, has a method for
toggling the boolean property in the Singleton and can launch FormB.
FormB has a PropertyGrid that is "bound" to the Singleton. Something
like this...
---start code---
public class Singleton
{
private static Singleton singleton = null;
public static Singleton GetInstance()
{
if(singleton == null)
{
singleton = new Singleton();
}
return singleton;
}
private bool myProperty;
private Singleton()
{
this.myProperty = true;
}
public bool MyProperty
{
get
{
return this.myProperty;
}
set
{
this.myProperty = value;
}
}
Quote:
}
public class FormA
{
public Singleton MySingleton;
public FormA()
{
MySingleton = Singleton.GetInstance();
}
private ToggleMyProperty()
{
MySingleton.MyProperty = !MySingleton.MyProperty;
}
Quote:
}
public class FormB
{
public FormB
{
this.propertyGrid.SelectedObject = MySingleton.GetInstance();
}
Quote:
}
---end code---
My problem is that no matter what I do to the property, the
PropertyGrid never displays the updated result. For example...
1. Run program
At this point MyProperty = true thanks to the Singleton's constructor.
2. FormB.Show
MyProperty is still true and the PropertyGrid on FormB shows this.
3. FormB.Close
MyProperty is still true.
4. Call ToggleMyProperty
Now MyProperty = false and I can see this by printing the value to the
console.
5. FormB.Show
Remember that MyProperty = false, but the PropertyGrid still shows the
value as true.
I've tried everything I can think of. I've called the Refresh method
on the PropertyGrid. I've set the PropertyGrid's SelectedObject to
null and back to the object. Nothing I've done so far has worked and
so I can only assume that it's just some dumba** mistake on my part.
It's almost like the PropertyGrid is "stuck" on the initial values.
Like I said, if I call the ToggleMyProperty method and then print the
results then I always get what I expect. Also, if I do this from
FormA...
MySingleton.GetInstance().MyProperty =
!MySingleton.GetInstance().MyProperty;
...then the PropertyGrid updates correctly. With that in mind, I have
a sneaking suspicion that the statement I'm using to set the
PropertyGrid's SelectedObject is at fault. I would just use
SelectedObject = MySingleton, but I can't since FormB has no idea that
MySingleton exists.
Any takers?
-Shuttermutt