Completed
Last Updated: 14 Jun 2018 14:41 by ADMIN
ADMIN
Hristo
Created on: 15 Feb 2018 18:55
Category:
Type: Bug Report
0
FIX. RadDropDownList - incorrect selection of items with duplicated text but different values when opening and closing the popup with the keyboard
How to reproduce: Close the popup using the keyboard and the Enter key

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        this.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        List<Item> items = new List<Item>();

        items.Add(new Item(0, "Data " + 0));
        items.Add(new Item(1, "Data " + 1));
        items.Add(new Item(2, "Data " + 2));
        items.Add(new Item(3, "Data " + 0));
        items.Add(new Item(4, "Data " + 4));
        items.Add(new Item(5, "Data " + 5));
        items.Add(new Item(6, "Data " + 0));

        this.radDropDownList1.DataSource = items;
        this.radDropDownList1.DisplayMember = "Description";
        this.radDropDownList1.ValueMember = "Id";
    }
}

public class Item
{
    public int Id { get; set; }

    public string Description { get; set; }

    public Item(int id, string description)
    {
        this.Id = id;
        this.Description = description;
    }
}


Workaround: 
public class MyRadDropDownList : RadDropDownList
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadDropDownList).FullName;
        }
    }

    public bool SuspenSelectedIndexChanging { get; internal set; }

    protected override RadDropDownListElement CreateDropDownListElement()
        {
            return new MyRadDropDownListElement();
        }

    protected override bool OnSelectedIndexChanging(object sender, int newIndex)
        {
            if (this.SuspenSelectedIndexChanging)
            {
                return true;
            }

            return base.OnSelectedIndexChanging(sender, newIndex);
        }
}

public class MyRadDropDownListElement : RadDropDownListElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadDropDownListElement);
        }
    }

    protected override RadPopupControlBase CreatePopupForm()
    {
        this.Popup = new MyDropDownPopupForm(this);
        this.Popup.VerticalAlignmentCorrectionMode = AlignmentCorrectionMode.SnapToOuterEdges;
        this.Popup.SizingMode = this.DropDownSizingMode;
        this.Popup.Height = this.DropDownHeight;
        this.Popup.HorizontalAlignmentCorrectionMode = AlignmentCorrectionMode.Smooth;
        this.Popup.RightToLeft = this.RightToLeft ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.Inherit;
        this.WirePopupFormEvents(this.Popup);

        return Popup;
    }
}

public class MyDropDownPopupForm : DropDownPopupForm
{
    MyRadDropDownList control;
    int selectedIndexOnPopupOpen = -1;

    public MyDropDownPopupForm(RadDropDownListElement ownerDropDownListElement)
        : base(ownerDropDownListElement)
    {
        this.control = (MyRadDropDownList)(ownerDropDownListElement.ElementTree.Control);
    }

    public override void ShowPopup(Rectangle alignmentRectangle)
    {
        base.ShowPopup(alignmentRectangle);

        this.selectedIndexOnPopupOpen = this.OwnerDropDownListElement.SelectedIndex;
    }

    public override void ClosePopup(PopupCloseInfo info)
    {
        base.ClosePopup(info);

        this.selectedIndexOnPopupOpen = -1;
    }

    public override bool OnKeyDown(Keys keyData)
    {
        RadListDataItem hoveredItem = this.GetSelectedOrHoveredItem();
        
        if (keyData == Keys.Enter && hoveredItem == null)
        {
            this.control.SuspenSelectedIndexChanging = true;
        }
        
        bool baseKeyDown = base.OnKeyDown(keyData);
        this.control.SuspenSelectedIndexChanging = false;
        return baseKeyDown;
    }
    
    private RadListDataItem GetSelectedOrHoveredItem()
    {
        RadListDataItem hoveredItem = null;
        RadElementCollection items = this.OwnerDropDownListElement.ListElement.ViewElement.Children;
        foreach (RadListVisualItem item in items)
        {
            if (item.Selected && item.Data.RowIndex != selectedIndexOnPopupOpen)
            {
                return item.Data;
            }

            if (item.IsMouseOver)
            {
                hoveredItem = item.Data;
            }
        }

        return hoveredItem;
    }
}
1 comment
ADMIN
Dimitar
Posted on: 25 May 2018 06:03
This can be reproduced when the autocomplete is enabled as well: 
public RadForm1()
{
    InitializeComponent();
    this.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
    List<Item> items = new List<Item>();

    items.Add(new Item(0, "Data " + 0));
    items.Add(new Item(1, "Data " + 1));
    items.Add(new Item(2, "Data " + 2));
    items.Add(new Item(3, "Data " + 0));
    items.Add(new Item(4, "Data " + 4));
    items.Add(new Item(5, "Data " + 5));
    items.Add(new Item(6, "Data " + 0));

    this.radDropDownList1.DataSource = items;
    this.radDropDownList1.DisplayMember = "Description";
    this.radDropDownList1.ValueMember = "Id";
    this.radDropDownList1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}

private void radButton1_Click(object sender, EventArgs e)
{
    Console.WriteLine(this.radDropDownList1.SelectedValue);
}