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

SelectedIndexChanged problem

1 Answer 400 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Lasse
Top achievements
Rank 1
Lasse asked on 12 Apr 2012, 09:56 AM
Hi,

I have an RadDropDownList filled with some items using DataSource and bounded functionality.
When I in the SelectedIndexChanged event tries to reset the selectedvalue to the original (the one before i changed), the control simply wont listen to my commands ;) 

This is the code I use in the SelectedIndexChanged event:
void AgreementRadDropDownSelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
    if (_init || _selectedAgreement == (int)AgreementRadDropDown.SelectedValue) return;
 
    if (MessageBox.Show("Do you want to change the agreement?", "Change Agreement", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
    {
        AgreementRadDropDown.SelectedValue = _selectedAgreement;
        return;
    }
    _selectedAgreement = (int)AgreementRadDropDown.SelectedValue;
}

The rest of the code of the form is here:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace TestDropDownList
{
    public partial class Form1 : Form
    {
        private readonly IList<Agreement> _agreements = new List<Agreement>();
        private int _selectedAgreement;
        private readonly bool _init = false;
 
        public Form1()
        {
            _init = true;
            InitializeComponent();
            AgreementRadDropDown.ItemDataBound += AgreementRadDropDownItemDataBound;
            AgreementRadDropDown.SelectedIndexChanged += AgreementRadDropDownSelectedIndexChanged;
            LoadAgreements();
            _selectedAgreement = 2;
 
            AgreementRadDropDown.SuspendUpdate();
            AgreementRadDropDown.DisplayMember = "Name";
            AgreementRadDropDown.ValueMember = "Id";
            AgreementRadDropDown.DataSource = _agreements;
 
            AgreementRadDropDown.SelectedValue = _selectedAgreement;
            AgreementRadDropDown.ResumeUpdate();
            _init = false;
        }
 
        void AgreementRadDropDownSelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (_init || _selectedAgreement == (int)AgreementRadDropDown.SelectedValue) return;
 
            if (MessageBox.Show("Do you want to change the agreement?", "Change Agreement", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
            {
                AgreementRadDropDown.SelectedValue = _selectedAgreement;
                return;
            }
            _selectedAgreement = (int)AgreementRadDropDown.SelectedValue;
        }
 
        private void LoadAgreements()
        {
            _agreements.Add(new Agreement(1, "Agreement1", Properties.Resources._lock));
            _agreements.Add(new Agreement(2, "Agreement2", Properties.Resources._lock));
            _agreements.Add(new Agreement(3, "Agreement3", Properties.Resources.lock_unlock));
            _agreements.Add(new Agreement(4, "Agreement4", Properties.Resources._lock));
            _agreements.Add(new Agreement(5, "Agreement5", Properties.Resources.lock_unlock));
            _agreements.Add(new Agreement(6, "Agreement6", Properties.Resources._lock));
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
             
        }
 
        static void AgreementRadDropDownItemDataBound(object sender, ListItemDataBoundEventArgs args)
        {
            var item = args.NewItem.DataBoundItem as Agreement;
            if (item != null)
                args.NewItem.Image = item.StatusImage;
        }
    }
 
    public class Agreement
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Image StatusImage { get; set; }
 
        public Agreement(int id, string name, Image statusImage)
        {
            Id = id;
            Name = name;
            StatusImage = statusImage;
        }
    }
}
namespace TestDropDownList
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.AgreementRadDropDown = new Telerik.WinControls.UI.RadDropDownList();
            ((System.ComponentModel.ISupportInitialize)(this.AgreementRadDropDown)).BeginInit();
            this.SuspendLayout();
            //
            // AgreementRadDropDown
            //
            this.AgreementRadDropDown.DropDownAnimationEnabled = true;
            this.AgreementRadDropDown.Location = new System.Drawing.Point(48, 37);
            this.AgreementRadDropDown.Name = "AgreementRadDropDown";
            this.AgreementRadDropDown.ShowImageInEditorArea = true;
            this.AgreementRadDropDown.Size = new System.Drawing.Size(222, 20);
            this.AgreementRadDropDown.TabIndex = 0;
            this.AgreementRadDropDown.Text = "radDropDownList1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(318, 167);
            this.Controls.Add(this.AgreementRadDropDown);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.AgreementRadDropDown)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadDropDownList AgreementRadDropDown;
    }
}


I use two images in my project.. they are attached here and need to be added to the resource file of the project to make this look like the one i have here :)

What am i doing wrong?

Regards
Svein Thomas

1 Answer, 1 is accepted

Sort by
0
Accepted
Peter
Telerik team
answered on 17 Apr 2012, 07:38 AM
Hello Svein,

Thank you for contacting Telerik support.

You cannot change SelectedIndex and SelectedValue properties in SelectingIndexChanging/SelectedIndexChanged events. For you case I would like to propose to Cancel the SelectingIndexChanging event:
void AgreementRadDropDownSelectedIndexChanging(object sender, Telerik.WinControls.UI.Data.PositionChangingCancelEventArgs e)
            {
                if (_init || _selectedAgreement == (int)AgreementRadDropDown.SelectedValue) return;
 
                if (MessageBox.Show("Do you want to change the agreement?", "Change Agreement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                    return;
                }
                _selectedAgreement = (int)AgreementRadDropDown.SelectedValue;
            }

Please, refer to the modified project.

I hope this helps. Don't hesitate to contact us if you have other questions.

All the best,
Peter
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
DropDownList
Asked by
Lasse
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or