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

Find RadDropDownList from KeyDown event

2 Answers 162 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 30 Oct 2015, 06:17 PM

Hello,

 I am dynamically creating a RadDropDownList and I add a handler to the KeyDown event. When this event gets called the sender is a RadDropDownListElement. How can I find the RadDropDownList that the element belongs to? I've tried the obvious properties like "parent" (It is a RadElement) with no luck.

 Thanks!

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 02 Nov 2015, 01:36 PM
Hi David,

Thank you for writing.

The RadDropDownList control serves as a wrapper of the RadDropDownListElement. Eventually, events and all the functionality are implemented by the element and then transferred on the control. If you would like to distinguish the created at run time RadDropDownList instances in the handler of an event in which you only receive the element as an argument, you would actually need to distinguish the elements. The easiest solution is to store some data in their Tag property. Please check my code snippet below: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    private void radButton1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            RadDropDownList ddl = new RadDropDownList();
            for (int j = 0; j < 10; j++)
            {
                ddl.Items.Add(string.Format("Item {0} {1}", i, j));
            }
 
            ddl.Name = "DDL " + i;
            ddl.Parent = this;
            ddl.Location = new Point(ddl.Width * i, 0);
            ddl.DropDownListElement.Tag = ddl.Name;
 
            ddl.KeyDown += ddl_KeyDown;
        }
    }
 
    private void ddl_KeyDown(object sender, KeyEventArgs e)
    {
        RadMessageBox.Show(((RadDropDownListElement)sender).Tag.ToString());
    }
}

Additional information about the control element structure of TPF is available here: 
Introducing the Telerik Presentation Framework. I am also sending you a gif file showing the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 02 Nov 2015, 02:09 PM
Thank you Hristo, that was exactly what I needed.
Tags
DropDownList
Asked by
David
Top achievements
Rank 1
Answers by
Hristo
Telerik team
David
Top achievements
Rank 1
Share this question
or