Search code examples
winformstelerikpropertygrid

How to add progress bar to Telerik RadPropertyGrid?


I use Telerik WinForms package. My RadPropertyGrid displays some settings:

radPropertyGrid1.SelectedObject = m_db;
radPropertyGrid1.ReadOnly = true;

How I can add a new item as a ProgressBar to show percent and color? It is static field, no need to change anything dynamically.

Or as a 2nd way: how to add colored field with %?


Solution

  • In order to display a progress bar in RadPropertyGrid, the possible solution is to use custom items. RadPropertyGrid allows you to create and use your own custom value items, allowing you to add the desired editors to fit your business need. More information on this topic you can find here: https://docs.telerik.com/devtools/winforms/controls/propertygrid/custom-items

    For your reference, I prepared a sample demonstration of how you can achieve your requirement. Note that this is a sample demonstration and may not cover all possible cases. Feel free to modify or extend it as per your specific needs.

    Please refer to the following code snippet:

    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            PropertyStoreItem stringItem = new PropertyStoreItem(typeof(string), "String", "Telerik", "Property storing a string value", "Telerik");
            RadPropertyStore store = new RadPropertyStore();
            store.Add(stringItem);
            PropertyStoreItem progressItem = new PropertyStoreItem(typeof(int), "Progress", 40, "Represents a progress bar element.");
            store.Add(progressItem);
            this.radPropertyGrid1.SelectedObject = store;
    
            this.radPropertyGrid1.CreateItemElement += this.RadPropertyGrid1_CreateItemElement;
        }
    
        private void RadPropertyGrid1_CreateItemElement(object sender, CreatePropertyGridItemElementEventArgs e)
        {
            if (e.ItemElementType == typeof(PropertyGridItemElement))
            {
                if (e.Item.Name == "Progress")
                {
                    e.ItemElementType = typeof(CustomItemElement);
                }
                else
                {
                    e.ItemElementType = typeof(DefaultPropertyGridItemElement);
                }
            }
        }
    }
    
    
    public class CustomPropertyGridValueElement : PropertyGridValueElement
    {
        RadProgressBarElement progressBarElement;
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            progressBarElement = new RadProgressBarElement();
            this.Children.Add(progressBarElement);
        }
    
        public override void Synchronize()
        {
            PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
    
    
            if (item != null && item.Value != DBNull.Value)
            {
                this.progressBarElement.Value1 = Convert.ToInt16(item.Value);
            }
        }
    }
    
    public class CustomItemElement : PropertyGridItemElement
    {
        protected override PropertyGridValueElement CreatePropertyGridValueElement()
        {
            return new CustomPropertyGridValueElement();
        }
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(PropertyGridItemElement);
            }
        }
        public override bool IsCompatible(PropertyGridItemBase data, object context)
        {
            return data.Label == "Progress";
        }
    }
    public class DefaultPropertyGridItemElement : PropertyGridItemElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(PropertyGridItemElement);
            }
        }
        public override bool IsCompatible(PropertyGridItemBase data, object context)
        {
            return data.Label != "Progress";
        }
    }
    

    enter image description here