This is a migrated thread and some comments may be shown as answers.

PropertyValueChanged Exception

5 Answers 258 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Anton
Top achievements
Rank 1
Anton asked on 23 Apr 2012, 12:41 PM
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

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 24 Apr 2012, 03:43 PM
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
0
Anton
Top achievements
Rank 1
answered on 25 Apr 2012, 02:06 PM
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?

0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Apr 2012, 02:55 PM
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));
            }
 
        }
 
 
    }
}
0
Anton
Top achievements
Rank 1
answered on 26 Apr 2012, 09:22 AM
Thanks a lot.
0
Ivan Petrov
Telerik team
answered on 26 Apr 2012, 09:54 AM
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 >>
Tags
PropertyGrid
Asked by
Anton
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Anton
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or