none
PropertyGrid PropertyValueChanged SetValue not working in .NET 4.0 RRS feed

  • Question

  • I have seen great examples here and had great hopes that it would work for our application. I need to catch a change in the GridItem and revert it under certain conditions. We are using VB.Net 2012 with .NET Framework 4.0. All the examples I have been able to find use the following code:

    Code:
    Dim iChangedItem = e.ChangedItem
    Dim propertyName = iChangedItem.PropertyDescriptor.Name
    Dim pInfo As _PropertyInfo = pgSetup.SelectedObject.GetType().GetProperty(propertyName)
    pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, null)
    If I do this I get the following message from VisualStudio:

    'null' is not declared. 'Null' constant is no longer supported. Use 'System.DBNull' instead.

    If I use 'System.DBNull.Value' I get a different error:

    Value of type 'System.DBNull' cannot be converted to '1-dimensional array of Object'

    I have also tried 'Nothing", which VisualStudio is OK with but I get a runtime error of:

    NullReferenceException was unhandled

    Any ideas how I can get this to work? I know that there is a version with only 2 parameters in later versions of the .NET Framework, but as of today anyway, I cannot change the .NET platform for this program.

    Tim

    Monday, August 20, 2018 6:19 PM

Answers

  • With the help from a user in another Forum, this is the solution that ended up working:

    Dim propertyName = e.ChangedItem.PropertyDescriptor.Name
    Dim pInfo As PropertyInfo = pgSetup.SelectedGridItem.Parent.Value.GetType().GetProperty(propertyName)
    
    pInfo.SetValue(pgSetup.SelectedGridItem.Parent.Value, e.OldValue, Nothing)

    Tim

    • Marked as answer by Tim8w Wednesday, August 22, 2018 2:36 PM
    Wednesday, August 22, 2018 2:35 PM
  •  You can just use the SetValue Method of the PropertyDescriptor to do the same thing.  This would be just a little easier,  and remove the need to use Reflections.  For example,  I created a form project that targets .Net Framework 4.0  and set up a PropertyGrid with an Expandable property,  then used the below code to get the result you are after,  resetting the property back to the old value.

        Private Sub pgSetup_PropertyValueChanged(sender As Object, e As PropertyValueChangedEventArgs) Handles pgSetup.PropertyValueChanged
            e.ChangedItem.PropertyDescriptor.SetValue(e.ChangedItem.Parent.Value, e.OldValue)
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, August 22, 2018 6:43 PM
    • Marked as answer by Tim8w Wednesday, August 22, 2018 6:52 PM
    Wednesday, August 22, 2018 6:40 PM

All replies

  • Can you indicate show were the code is in e.g. an event ???

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Monday, August 20, 2018 6:34 PM
    Moderator
  • Hi,

    try the code:

    Imports System.Reflection
    
    Public Class Form1
        Private Sub prg_PropertyValueChanged(s As Object, e As PropertyValueChangedEventArgs) Handles prg.PropertyValueChanged
            Dim iChangedItem = e.ChangedItem
            Dim propertyName = iChangedItem.PropertyDescriptor.Name
            Dim pInfo As PropertyInfo = prg.SelectedObject.GetType().GetProperty(propertyName)
            pInfo.SetValue(prg.SelectedObject, e.OldValue, Nothing)
        End Sub
    End Class

    Try it, will you report an error?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 21, 2018 5:53 AM
  • OK. I have determined that the PropertyInfo is Nothing for some reason. Here is the entire code. Any ideas on to why the PropertyInfo would return Nothing?

    Code:
    Private Sub pgSetup_PropertyValueChanged(sender As System.Object, e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles pgSetup.PropertyValueChanged
    
    Dim iChangedItem = e.ChangedItem
    Dim propertyName = iChangedItem.PropertyDescriptor.Name
    Dim pInfo As PropertyInfo = pgSetup.SelectedObject.GetType().GetProperty(propertyName)
    
    pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, Nothing)
    
    End Sub

    Tim

    Tuesday, August 21, 2018 1:55 PM
  •  I don't seem to be able to reproduce the problem.  What type of property are you changing in the PropertyGrid when this exception is thrown?  If you could give us a small (working) example that will reproduce the same exception it would help.  8)

    If you say it can`t be done then i`ll try it

    Tuesday, August 21, 2018 5:28 PM
  • Like I mentioned above, the only reason that I'm getting an exception now is because the PropertyInfo is Nothing for some reason. All the fields in the PropertyGrid show Nothing for their PropertyInfo. I just don't get it. This one in particular is a simple Boolean! It gets the PropertyName just fine, but when I attempt to get the PropertyInfo, it returns 'Nothing'. Maybe it has something to do with .Net 4.0? I hope not because I can't change that for this project right now.

    Tim



    • Edited by Tim8w Tuesday, August 21, 2018 9:36 PM typos
    Tuesday, August 21, 2018 5:38 PM
  • Like I mentioned above, the only reason that I'm getting an exception now is because the PropertyInfo is Nothing for some reason. All the fields in the PropertyGrid show Nothing for their PropertyInfo. I just don'tget it. This one in particular is a simple Boolean! It gets the PropertyName just fine, but when I attempt to get the PropertyInfo, it returns 'Nothing'. Maybe it has something to do with .Net 4.0? I hope not because I can't change that for this project right now.

    Tim



     It does not appear that targeting 4.0 is a problem.  This boolean type property,  is it in an expandable property by any chance,  or maybe is the property defined as an array of Boolean type?  Just wondering because I did manage to get the same problem with a property that was an array type.  I did not find the solution to that yet though.

    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, August 21, 2018 9:41 PM
    Tuesday, August 21, 2018 9:40 PM
  • I don't think so. When I select it, I do have the choice of either "True" or "False". But like I said before, all the items come up as 'Nothing' regardless of the type even ones that take an Integer.

    


    Tim

    Tuesday, August 21, 2018 10:51 PM
  • With the help from a user in another Forum, this is the solution that ended up working:

    Dim propertyName = e.ChangedItem.PropertyDescriptor.Name
    Dim pInfo As PropertyInfo = pgSetup.SelectedGridItem.Parent.Value.GetType().GetProperty(propertyName)
    
    pInfo.SetValue(pgSetup.SelectedGridItem.Parent.Value, e.OldValue, Nothing)

    Tim

    • Marked as answer by Tim8w Wednesday, August 22, 2018 2:36 PM
    Wednesday, August 22, 2018 2:35 PM
  •  Yes,  it appears that your PartConfiguration property is an expandable property and the RatiometricAdjust property is a child property of it.  An expandable property is one that has the little arrow at the left and can be expanded to see the child properties of it,  or collapsed to hide them.  This is why I was trying to figure out what type of property you where dealing with.  Anyways,  glad you got it solved.  8)

    If you say it can`t be done then i`ll try it

    Wednesday, August 22, 2018 4:25 PM
  •  You can just use the SetValue Method of the PropertyDescriptor to do the same thing.  This would be just a little easier,  and remove the need to use Reflections.  For example,  I created a form project that targets .Net Framework 4.0  and set up a PropertyGrid with an Expandable property,  then used the below code to get the result you are after,  resetting the property back to the old value.

        Private Sub pgSetup_PropertyValueChanged(sender As Object, e As PropertyValueChangedEventArgs) Handles pgSetup.PropertyValueChanged
            e.ChangedItem.PropertyDescriptor.SetValue(e.ChangedItem.Parent.Value, e.OldValue)
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, August 22, 2018 6:43 PM
    • Marked as answer by Tim8w Wednesday, August 22, 2018 6:52 PM
    Wednesday, August 22, 2018 6:40 PM