New to Telerik UI for WinFormsStart a free 30-day trial

Using Custom Editor

Updated over 6 months ago

This following snippet demonstrates how to create and replace the standard spin editor with a track bar editor.

Figure 1: TrackBar Editor

WinForms RadPropertyGrid TrackBar Editor

All property grid editors inherit from BaseInputEditor. So, you have to inherit from this class and override several methods:

Creating Custom Editor

C#
public class PropertyGridTrackBarEditor : BaseInputEditor
{
    public override object Value
    {
        get
        {
            RadTrackBarElement editor = (RadTrackBarElement)this.EditorElement;
            return editor.Value;
        }
        set
        {
            RadTrackBarElement editor = (RadTrackBarElement)this.EditorElement;
            if (value != null && value != DBNull.Value)
            {
                editor.Value = Convert.ToInt32(value);
            }
            else
            {
                editor.Value = 0;
            }
        }
    }
    public override void BeginEdit()
    {
        base.BeginEdit();
        this.EditorElement.Focus();
        ((RadTrackBarElement)this.EditorElement).ValueChanged += new EventHandler(TrackBarEditor_ValueChanged);
    }
    void TrackBarEditor_ValueChanged(object sender, EventArgs e)
    {
        PropertyGridItemElement owner = this.OwnerElement as PropertyGridItemElement;
        if (owner != null)
        {
            owner.PropertyTableElement.RaiseValueChanged(EventArgs.Empty);
        }
    }
    public override bool EndEdit()
    {
        ((RadTrackBarElement)this.EditorElement).ValueChanged -= TrackBarEditor_ValueChanged;
        return base.EndEdit();
    }
    protected override RadElement CreateEditorElement()
    {
        return new RadTrackBarElement();
    }
    public override Type DataType
    {
        get { return typeof(int); }
    }
}

The EditorRequired event is the correct place to replace the default editor:

Replace Editor

C#
void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(PropertyGridSpinEditor))
    {
        e.EditorType = typeof(PropertyGridTrackBarEditor);
    }
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support