PropertyValueChanged Exception

6 posts, 1 answers
  1. Anton
    Anton avatar
    5 posts
    Member since:
    Feb 2012

    Posted 23 Apr 2012 Link to this post

    Hi. I'm working with RadPropertyGrid for WinForms. 
    I have some problem with PropertyValueChanged Event
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                radPropertyGrid1.PropertyValueChanged += new PropertyGridItemValueChangedEventHandler(radPropertyGrid1_PropertyValueChanged);
                string A = "";
                int B = 0;
                bool C = false;
                RadPropertyStore store = new RadPropertyStore();
                PropertyStoreItem itemA = new PropertyStoreItem(typeof(string), "A", A);
                PropertyStoreItem itemB = new PropertyStoreItem(typeof(int), "B", B);
                PropertyStoreItem itemC = new PropertyStoreItem(typeof(bool), "C", C);
                store.Add(itemA);
                store.Add(itemB);
                store.Add(itemC);
                radPropertyGrid1.SelectedObject = store;
            }
      
            void radPropertyGrid1_PropertyValueChanged(object sender, 
                                         PropertyGridItemValueChangedEventArgs e)
            {
                try
                {
                    throw new NotImplementedException();
                }
                catch(Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
        }
    So, how can you see, In my project I need show MessageBox with some Exception. When I changed bool value (itemC) in propertyGrid, MessegeBox is shown once and it's ok, but when I changed string or int value PropertyValueChanged event began twice (second event began in line "MessageBox.Show(exc.Message); ")
    Maybe you can explain me what I do wrong. Thanks
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Apr 2012 Link to this post

    Hello, 

    Please try this example as a workaround to your issue: 

    public Form1()
    {
        InitializeComponent();
     
        string A = "";
        int B = 0;
        bool C = false;
        RadPropertyStore store = new RadPropertyStore();
        PropertyStoreItem itemA = new PropertyStoreItem(typeof(string), "A", A);
        PropertyStoreItem itemB = new PropertyStoreItem(typeof(int), "B", B);
        PropertyStoreItem itemC = new PropertyStoreItem(typeof(bool), "C", C);
        store.Add(itemA);
        store.Add(itemB);
        store.Add(itemC);
        radPropertyGrid1.SelectedObject = store;
     
        itemA.PropertyChanged +=new PropertyChangedEventHandler(item_PropertyChanged);
        itemB.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        itemC.PropertyChanged +=new PropertyChangedEventHandler(item_PropertyChanged);
    }
     
    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        try
        {
            if (e.PropertyName == "Value")
            {
                throw new NotImplementedException();
            }
     
        }
        catch(Exception exc)
        {
            RadMessageBox.Show(exc.Message);
        }
     
    }

    Hope this helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Anton
    Anton avatar
    5 posts
    Member since:
    Feb 2012

    Posted 25 Apr 2012 Link to this post

    Hello. Thank you for answer. 
    Your solution work, but its work only for simple type item. 
    For example,
    [TypeConverter(typeof(ExpandableObjectConverter))] 
    public class SomeClass 
    {  
            public string name{ get; set; }   
            public int count{ get; set; }  
    ... 
    SomeClass value = new SomeClass(){ name = "A", count = 0 }; 
    PropertyStoreItem someItem = new PropertyStoreItem(typeof(SomeClass), "Value", value); 
    someItem.PropertyChanged += new PropertyChangedEventHandler(someItem_PropertyChanged); 
    ... 
    void someItem_PropertyChanged(object sender, PropertyChangedEventArgs e) 
         try
         {                 
             throw new NotImplementedException(); 
         
         catch (Exception exc) 
         
             System.Windows.Forms.MessageBox.Show(exc.Message); 
         
    }

    So, in this case, obviously, event isn't called when I change PropertyGridItem 'name' or 'count'. 
    What can I do to make this possible?

  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 25 Apr 2012 Link to this post

    Hi Anton, 

    You could always inherit from INotifyPropertyChanged

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : Form
        {
     
     
            public Form1()
            {
                InitializeComponent();
     
                SomeClass value = new SomeClass(){ Name = "A", Count = 0 };
                value.PropertyChanged += new PropertyChangedEventHandler(value_PropertyChanged);
                RadPropertyStore store = new RadPropertyStore();
                PropertyStoreItem someItem = new PropertyStoreItem(typeof(SomeClass), "Value", value);
                store.Add(someItem);
     
                this.radPropertyGrid1.SelectedObject = store;
            }
     
            void value_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                System.Windows.Forms.MessageBox.Show(e.PropertyName + " has changed");
            }
     
     
     
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public class SomeClass : INotifyPropertyChanged
            {
                public event PropertyChangedEventHandler PropertyChanged;
     
                private string _Name;
                private int _Count;
     
                public string Name
                {
                    get
                    {
                        return _Name;
                    }
                    set
                    {
                        _Name = value;
                        OnChanged("Name");
                    }
                }
     
                public int Count
                {
                    get
                    {
                        return _Count;
                    }
                    set
                    {
                        _Count = value;
                        OnChanged("Count");
                    }
                }
     
                public void Reset()
                {
                    this.Name = "A";
                    this.Count = 0;
                }
     
                private void OnChanged(string propName)
                {
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs(propName));
                }
     
            }
     
     
        }
    }
  6. Anton
    Anton avatar
    5 posts
    Member since:
    Feb 2012

    Posted 26 Apr 2012 Link to this post

    Thanks a lot.
  7. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 26 Apr 2012 Link to this post

    Hello Anton,

    Thank you for writing.

    Using your code I was able to reproduce the issue. This issue is already addressed in our internal build and it will be out with our next release (Q2 2012). Until then you can workaround it by modifying your code as follows:
    Timer timer = new Timer();
    bool show = true;
     
    public Form1()
    {
      InitializeComponent();
     
      //Same code here...
     
      this.timer.Interval = 1;
      this.timer.Tick += new EventHandler(timer_Tick);
    }
     
    private void timer_Tick(object sender, EventArgs e)
    {
      this.show = true;
      this.timer.Stop();
    }
     
    private void radPropertyGrid1_PropertyValueChanged(object sender, PropertyGridItemValueChangedEventArgs e)
    {
      try
      {
        throw new NotImplementedException();
      }
      catch (Exception exc)
      {
        if (this.show)
        {
          this.show = false;
          MessageBox.Show(exc.Message);
          this.timer.Start();
        }
      }
    }

    I have updated your Telerik points for bringing this issue to our attention.

    I hope this will be useful. Should you have further questions, I would be glad to help.
     
    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready