Find RadDropDownList from KeyDown event

3 posts, 0 answers
  1. David
    David avatar
    19 posts
    Member since:
    Feb 2013

    Posted 30 Oct 2015 Link to this post

    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. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 02 Nov 2015 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. David
    David avatar
    19 posts
    Member since:
    Feb 2013

    Posted 02 Nov 2015 in reply to Hristo Link to this post

    Thank you Hristo, that was exactly what I needed.
Back to Top