none
Input validation on property grid entry RRS feed

  • Question

  • I want to validate the input when a user changes a property on an object through a propertygrid.  In the code I am working on it is too complicated to check the input on the set/get property in the object's class, so instead I thought it would be best to use a TypeConverter with the property in question and handle the user input validation in the ConvertFrom method.  TypeConverter::CanConvertFrom gets called after user input (without the user inputed value), but TypeConverter::ConvertFrom (both overloaded forms) never seems to get called, so I am still left without an appropriate place to validate input.  Why isn't ConvertFrom getting called?  Is there a better way to do this?

    Other info: 
    -the property in question is of string type
    -my TypeConverter was inheriting from StringConverter
    -the property also has an associated editor
    -I am using .NET Framework 2.0
    Tuesday, March 9, 2010 7:04 PM

Answers

  • Hi, mduel25:

        Please check the following code snippets:

            String myfield;
    
            [TypeConverter(typeof(myPropertyConverter))]
            public String myProperty
            {
                get
                {
                    return myfield;
                }
                set
                {
                    myfield = value;
                }
            }
    
    
        public class myPropertyConverter : StringConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                if ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType))
                    return true;
                else return false;
            }
    
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                String field=Convert.ToString(value);
    
                if (!validate(field))
                {
                    throw new Exception("The value should be 'criteria'");
                }
                return field;
            }
    
            private bool validate(String value)
            {
                if (value == "criteria") return true;
                else return false;
            }
        }
    

       And the following references could be helpful:

      1. How to: Implement a Type Converter

      2. PropertyGrid input validation   

      3. Make Your Components Really RAD with Visual Studio .NET Property Browser

      4. Property Grid - Dynamic List ComboBox, Validation, and More

     

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, March 23, 2010 6:06 AM
    Friday, March 19, 2010 8:31 AM

All replies

  • I ran into a similar issue the other day. What I did was subscribed to the propertygrid's PropertyValueChanged event. PropertyValueChangedEventArgs.ChangedItem will give the GridItem. From the GridItem you can get the value and then validate that. Not sure if that is what you were looking for but thought it might help.
    Tuesday, March 9, 2010 9:28 PM
  • This is not a solution because the object's value gets changed before execution reaches this event handler.  I need to make sure the input is valid before it affects the object.  Thanks though.
    Thursday, March 11, 2010 9:51 PM
  • Hi, mduel25:

        Please check the following code snippets:

            String myfield;
    
            [TypeConverter(typeof(myPropertyConverter))]
            public String myProperty
            {
                get
                {
                    return myfield;
                }
                set
                {
                    myfield = value;
                }
            }
    
    
        public class myPropertyConverter : StringConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                if ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType))
                    return true;
                else return false;
            }
    
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                String field=Convert.ToString(value);
    
                if (!validate(field))
                {
                    throw new Exception("The value should be 'criteria'");
                }
                return field;
            }
    
            private bool validate(String value)
            {
                if (value == "criteria") return true;
                else return false;
            }
        }
    

       And the following references could be helpful:

      1. How to: Implement a Type Converter

      2. PropertyGrid input validation   

      3. Make Your Components Really RAD with Visual Studio .NET Property Browser

      4. Property Grid - Dynamic List ComboBox, Validation, and More

     

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, March 23, 2010 6:06 AM
    Friday, March 19, 2010 8:31 AM
  • Hi, mduel25:

         I am writing to check the status of the problem.  Could you let us know your progress?


    Please mark the right answer at right time.
    Thanks,
    Sam
    Monday, March 22, 2010 2:36 AM