Need to change the value of DropDownList after SelectedIndex event

3 posts, 0 answers
  1. Ken
    Ken avatar
    6 posts
    Member since:
    Mar 2014

    Posted 20 May 2014 Link to this post

    Hi,

    I have some code that allows a user to trigger an action from a choice in a DropDownList.  After this action has completed, I would like to reset the index/value of the DropDownList to the first Item on the list (Index = 0).

    I am attempting to trigger the action on the SelectedIndexChanged event, but it will not allow me to change the index within the event.  I can change this value within another event, but I am not sure which one to use...   In the past, using Windows Forms, we have used the SelectionChangeCommitted event to do the reset, aftert the event was kicked off with the SelectedIndexChanged event.  What events/event order do I need to accomplish the above scenario?

    Thanks
    Laura
  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 21 May 2014 Link to this post

    Hello Laura,

    Thank you for writing.

    I think that the RadDropDownList.SelectedIndexChanging event is the appropriate one for your specific case. It is possible to determine which is the new item's index and perform the necessary action. Afterwards, cancel the event in order to keep the selection unchanged. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
        this.radDropDownList1.SelectedIndex = 0;
        this.radDropDownList1.SelectedIndexChanging += radDropDownList1_SelectedIndexChanging;
    }
     
    private void radDropDownList1_SelectedIndexChanging(object sender, PositionChangingCancelEventArgs e)
    {
        switch (e.Position)
        {
            default:
                //perform action according to the selected item's index
                RadMessageBox.Show(string.Format("Selected item at index {0}", e.Position));
                break;
        }          
        e.Cancel = true;
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ken
    Ken avatar
    6 posts
    Member since:
    Mar 2014

    Posted 21 May 2014 Link to this post

    Hi Desislava,

    This is working well.  I would just like to add that during the action I am kicking off, I am referencing the index of the selected drop down.  I realized that I have to reference the e.Position to obtain that value.  This is the statement I am using:
    radDropDownList1.Items[e.Position] to represent the list item, then I can get the Text and/or Value as well.

    Thank you for your help!

    Laura
Back to Top