This is a migrated thread and some comments may be shown as answers.
SelectedIndexChanging Event?
2 Answers 45 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
SUNIL
Top achievements
Rank 2
SUNIL asked on 23 Sep 2010, 12:16 AM
Is there any way to implement a 'SelectedIndexChanging' event for a RadComboBox?
The reason is that I need to cancel the index changed event sometimes, but I cannot since the changing event is missing in RadComboBox.

2 Answers, 1 is accepted

Sort by
0
SUNIL
Top achievements
Rank 2
answered on 24 Sep 2010, 04:46 PM
I was able to extend the radcombobox so that the selectedindexchanging event fires that allows the developer to cancel the selectedindexchanged event.
This will be useful for developers using Telerik RadControls for Winforms version prior to 2010 Q2.
The attached project contains an extended custom control that inherits RadComboBox but adds new functionality of selectedindexchanging event.

The complete code for such a custom control is given below.

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.UI;
using Telerik.WinControls;
 
 
 
namespace MyControls
{
    public partial class RadComboBoxPlus : RadComboBox
    {
        public event CancelEventHandler SelectedIndexChanging;
 
        [Browsable(false)]
        public int PreviousSelectedIndex { get; private set; }
 
        public RadComboBoxPlus()
        {
            InitializeComponent();
            PreviousSelectedIndex = -1;
        }
        public override string ThemeClassName
        {   
            get
            
                return typeof(RadComboBox).FullName; 
            
        }
 
        protected void OnSelectedIndexChanging(CancelEventArgs e)
        {
            var selectedIndexChanging = SelectedIndexChanging;
            if (selectedIndexChanging != null)
                selectedIndexChanging(this, e);
        }
 
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            if (PreviousSelectedIndex != SelectedIndex)
            {
                var cancelEventArgs = new CancelEventArgs();
                OnSelectedIndexChanging(cancelEventArgs);
                if (!cancelEventArgs.Cancel)
                {
                    PreviousSelectedIndex = SelectedIndex;
                    base.OnSelectedIndexChanged(e);
                }
                else
                    SelectedIndex = PreviousSelectedIndex;
            }
        }
 
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}

0
Peter
Telerik team
answered on 28 Sep 2010, 11:19 AM
Hello Sunil,

Thanks for the sharing this implementation.

Your approach is correct. You can also use RadDropDownList that implements this event.
Do not hesitate to contact us if you have other questions.

Best wishes,
Peter
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Asked by
SUNIL
Top achievements
Rank 2
Answers by
SUNIL
Top achievements
Rank 2
Peter
Telerik team
Share this question
or