Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PropertyGrid Validation

12 views
Skip to first unread message

Nick Carter

unread,
Mar 19, 2003, 11:36:33 AM3/19/03
to
I want to be able to change the way the PropertyGrid handles validation of
values entered into the grid and I don't see an obvious way to do this.

I use the PropertyGrid control in my applications. The user can enter values
into the PropertyGrid for the properties which are displayed in the
PropertyGrid. The properties have validation in their set accessors which
throw an exception if the values that the user enters are wrong. So if the
user enters a bad value the PropertyGrid catches the exception that I throw
and displays the "Invalid property value" dialog box. What I want to do is
handle the exception myself. I thought that I might override the
UITypeEditor.EditValue method something like this:-

public class TestDialogEditor : System.Drawing.Design.UITypeEditor

{

public override object
EditValue(System.ComponentModel.ITypeDescriptorContext context,
System.IServiceProvider provider, object value)

{

IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)
provider.GetService(typeof(IWindowsFormsEditorService));

if( edSvc == null )

return null;

try
{
value = DoTheDefaultEdit();
}
catch
{
HandleTheExceptionMyself();
}

return value;


}


The problem with this is that I don't see any way to implement the mythical
DoTheDefaultEdit().

Any ideas how I can either implement this method or solve this problem in a
completely different way ?

Nick.


Steve Turton

unread,
Mar 20, 2003, 5:41:51 AM3/20/03
to
Hope this link helps...

http://www.dotnet247.com/247reference/msgs/25/129425.aspx

"Nick Carter" <ni...@nospamplease.com> wrote in message
news:OWV3NXj7...@TK2MSFTNGP10.phx.gbl...

Nick Carter

unread,
Mar 20, 2003, 9:37:56 AM3/20/03
to
Steve,

Thanks. In the end it was the PropertyGrid.PropertyValueChanged event which
did what I wanted.

Nick.

"Steve Turton" <steve_...@lineone.net> wrote in message
news:OKEkn0s7...@TK2MSFTNGP11.phx.gbl...

Cosmin Smeu

unread,
Mar 20, 2003, 6:30:05 PM3/20/03
to
Greetings everyone,

I am using the PropertyGrid component in my application and I would like to
substitute my own message box for the message box that the component pops-up
whenever a property value contains invalid data (eg. alphanumeric characters
for an integer property).

The message box in question used by the component has:
- "Properties Window" as the title
- "Invalid property value" as the message.
- Details, OK, and Cancel buttons

I consider this message box to be user unfriendly for an application that's
meant to be used by non-programmers.

I tried to play with TypeConverters and do the checking there myself, but it
ends up poping 2 of my message boxes instead of one!

Thank you very much in advance!

Regards,
Cosmin


"Nick Carter" <ni...@nospamplease.com> wrote in message
news:OWV3NXj7...@TK2MSFTNGP10.phx.gbl...

Steve Turton

unread,
Mar 22, 2003, 3:40:32 AM3/22/03
to
I found that as well. Using :
Throw New ArgumentException("My Friendly Message")
in the ConvertFrom function.

Still has the rubbish message box but a nice 'details' message - but your
probably doing this, which is what I am doing. Even if I could solve the
double messagebox problem, I couldn't revert the value back to what it was
before the error(like pressing escape from the above message box).

Can't help on this - Sorry, I would like to know if you solve it though.

Steve.


"Cosmin Smeu" <Cosmi...@marconi.com> wrote in message
news:eegLtiz7...@TK2MSFTNGP11.phx.gbl...

Robert Grubbs

unread,
Mar 24, 2003, 11:58:56 AM3/24/03
to
I managed to replace that dialog, but it was one of the most vile,
evil things I've ever written.

Basically, when that dialog comes up I did a break and looked at the
call stack and dissassembly; after some hair pulling I figured out
that there is an internal property grid class
(System.Windows.Forms.PropertyGridInternal.PropertyGridView) that has
a member called ShowInvalidMessage. That method calls ShowDialog on a
GridErrorDlg object with the error message information. Fortunately,
ShowInvalidMessage tries to do a GetService call to retrieve an
IUIService on which to call ShowDialog. Normally that returns null,
but if you can manage to set the serviceProvider field of the
PropertyGridView to point to your own implementation of
IServiceProvider (which returns your own implementation of
IUIService), you can check the type of dialog being shown for
GridErrorDlg and display something else instead. Getting to the
PropertyGridView and its fields requires using reflection to bypass
the private/protected flags.

Needless to say, I don't recommend trying this unless you're really
bored or desperate to replace that dialog. I'd post my code, except
that my company owns it (and my soul).

Have a nice day,
Robert W. Grubbs

"Cosmin Smeu" <Cosmi...@marconi.com> wrote in message news:<eegLtiz7...@TK2MSFTNGP11.phx.gbl>...

Cosmin Smeu

unread,
Mar 24, 2003, 2:26:47 PM3/24/03
to
Thank you for the info!

I'll try to implement my own message box using the information you have
provided.
I am surprised Microsoft didn't take this into consideration, and make a
better design for customizing this aspect of the PropertyGrid UI.

I am afraid that in future versions of the .Net Framework, they might change
the internal implementation of the control, and cause us problems if we
implement this type of "hacking" in our commercial software.

Regards,
Cosmin

"Robert Grubbs" <demi...@yahoo.com> wrote in message
news:9ae5f5f3.03032...@posting.google.com...

Robert Grubbs

unread,
Mar 25, 2003, 9:30:22 AM3/25/03
to
It doesn't really suprise me that Microsoft didn't make the Property
Grid more flexible; it seems like a control more intended for use in
Visual Studio than user apps. However, it is just powerful enough to
be worth using and it is far better than writing my own (for the
moment).

Your comment about changes to the framework causing problems later for
this type of hack is quite right; I worry about that a lot, but the
limited customization available in the Framework classes leaves me
little choice sometimes. The major downside is that non-standardard
framework implementations may not work the same way, so my hack might
not work in Mono or Rotor, etc.

I changed my mind about posting code; hopefully someone can point out
any problems with my approach. here's a bare-bones version of what
I've done (I'm sure the wordwrapping will make a mess out of this):

public class PropertyGridEx : PropertyGrid
{
public PropertyGridEx() : base()
{
m_ServiceProxy=new PropertyGridServiceProxy(this);

//Use reflection to force the property grid to use our service
provider to get IUIService
Type propGridType=typeof(PropertyGrid);
System.Reflection.FieldInfo gvInfo=propGridType.GetField("gridView",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
object gridViewRef=gvInfo.GetValue(this);
System.Reflection.FieldInfo
spInfo=gvInfo.FieldType.GetField("serviceProvider",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
spInfo.SetValue(gridViewRef,m_ServiceProxy);
}
protected PropertyGridServiceProxy m_ServiceProxy;
protected override object GetService(Type service)
{
return GetServiceInternal(service);
}
public object GetServiceInternal(Type service)
{
if(service==typeof(IUIService))
{
return m_ServiceProxy;
}
return base.GetService(service);
}
}

public class PropertyGridServiceProxy : IServiceProvider, IUIService
{
public PropertyGridServiceProxy(PropertyGridEx propgrid)
{
m_PropertyGrid=propgrid;
}
protected PropertyGridEx m_PropertyGrid;

public object GetService(Type service)
{
return m_PropertyGrid.GetServiceInternal(service);
}

#region Implementation of IUIService
... <snip!> ... // Cut out the IUIService methods irrelevant to the
problem; they get forwarded to the framework's implementation

public System.Windows.Forms.DialogResult
ShowDialog(System.Windows.Forms.Form form)
{
//handle the case of the GridErrorDlg
Type propGridType=typeof(PropertyGrid);
System.Reflection.FieldInfo gvInfo=propGridType.GetField("gridView",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
object gridViewRef=gvInfo.GetValue(m_PropertyGrid);
System.Reflection.FieldInfo
edInfo=gvInfo.FieldType.GetField("errorDlg",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
if(form.GetType() == edInfo.FieldType)
{
object errorDlgRef=edInfo.GetValue(gridViewRef);
System.Reflection.PropertyInfo
msgInfo=edInfo.FieldType.GetProperty("Message");
System.Reflection.PropertyInfo
detInfo=edInfo.FieldType.GetProperty("Details");
string msg=(string)msgInfo.GetValue(errorDlgRef,null);
string det=(string)detInfo.GetValue(errorDlgRef,null);
//TODO: replace this with a pretty, colorful, friendly dialog that
gives users a warm fuzzy feeling
MessageBox.Show(m_PropertyGrid,msg + ": " + det,"Invalid Property
Value");
return DialogResult.OK;
}

//Other cases fall through to the framework's implementation
IUIService iUI=m_PropertyGrid.GetServiceExternal(typeof(IUIService))
as IUIService;
if(iUI==null)
return DialogResult.Cancel;

return iUI.ShowDialog(form);
}
#endregion
}

"Cosmin Smeu" <Cosmi...@marconi.com> wrote in message news:<uR5#Ztj8CH...@TK2MSFTNGP12.phx.gbl>...

0 new messages