Search code examples
c#winformstelerikraddropdownbutton

Telerik dropdown control not allowing me to select item from dropdown list


I have created a Telerik Test application with 4 controls( a button, 2 drop-down lists and a text box).

What I am trying to do is, on hitting the "Add project" button, the first drop-down list(project list from PopulateProjects() method) gets displayed. On selecting a project from this list, a next drop-down list with "project tasks from PopulateTasks() method" gets displayed. On selecting a task, the text box control displays the text content of both the project and task selected(TextDisplay() method).

The problem I am facing is, the drop-down controls do not wait for me to select an item, instead it just automatically selects the first item in the list as default. So the text box displays the first item in project list and project task i.e it displays "PROJECT abc TASK task one" . I am unable to even view the items in the drop-down list but I can see the items exist when I debug. The flow goes from radProjList.EndUpdate directly to the selectedIndexChanged. The event gets fired event though there was no change in index. The drop-down list does not even get displayed.

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnAddProject_Click(object sender, EventArgs e)
        {
            radProjList.Visible = true;
            radTaskList.Visible = false;
            PopulateProjects();
            radTaskList.Visible = false;
            radProjList.Visible = false;

        }

        public string NoteText
        {
            get
            {
                //return txtNote.Text;
                return radTextBox.Text;
            }
            set
            {
                //txtNote.Text = value;
                radTextBox.Text = value;
            }
        }

        void TextDisplay()
        {
            string projname = radProjList.SelectedText;
            string projtask = radTaskList.SelectedText;
            this.radTextBox.Text = "PROJECT "+projname + " TASK " + projtask;
            radTaskList.Visible = false;
            radProjList.Visible = false;
        }


        void PopulateProjects()
        {
            radProjList.Visible = true;
            radTaskList.Visible = false;
            radProjList.Items.Clear();
            radProjList.Text = "Select Project";
            List<string> ProjectName = new List<string>();
            ProjectName.Add("abc");
            ProjectName.Add("def");
            ProjectName.Add("ghi");
            ProjectName.Add("jkl");
            ProjectName.Add("mno");
            ProjectName.Add("pqr");

            radProjList.BeginUpdate();
            radProjList.DataSource = ProjectName;
            radProjList.DisplayMember = "ProjectName";
            radProjList.ValueMember = "ProjectName";
            radProjList.AutoCompleteDataSource = ProjectName;
            radProjList.DropDownListElement.AutoCompleteSuggest.SuggestMode = Telerik.WinControls.UI.SuggestMode.Contains;
            Size popupSize = new Size(400, 300);
            radProjList.DropDownListElement.AutoCompleteSuggest.DropDownList.DropDownMinSize = popupSize;
            radProjList.DropDownListElement.DropDownMinSize = popupSize;
            radProjList.ListElement.Font = new Font("Microsoft Sans Serif", 16);
            radProjList.DropDownListElement.AutoCompleteSuggest.DropDownList.Popup.Font = new System.Drawing.Font("Microsoft Sans Serif", 16);
            radProjList.EndUpdate();

            radProjList.SelectedIndex = -1;
            radProjList.Text = "Select Project";

        }
        void PopulateTasks()
        {
            List<string> populateTaskList = new List<string>();
            radTaskList.Visible = true;
            radTaskList.Items.Clear();
            populateTaskList.Add("task one");
            populateTaskList.Add("task two");
            populateTaskList.Add("task three");
            populateTaskList.Add("task four");
            populateTaskList.Add("task five");
            populateTaskList.Add("task six");

            radTaskList.Items.Clear();
            radTaskList.Text = "Select Tasks";
            radTaskList.BeginUpdate();
            radTaskList.DataSource = populateTaskList;
            radTaskList.DisplayMember = "projectTask";
            radTaskList.ValueMember = "projectTask";
            radTaskList.AutoCompleteDataSource = populateTaskList;
            radTaskList.AutoCompleteMode = AutoCompleteMode.Suggest;
            Size popupSize = new Size(400, 300);
            radTaskList.DropDownListElement.AutoCompleteSuggest.DropDownList.DropDownMinSize = popupSize;
            radTaskList.DropDownListElement.DropDownMinSize = popupSize;
            radTaskList.DropDownListElement.AutoCompleteSuggest.DropDownList.ListElement.VisualItemFormatting += new Telerik.WinControls.UI.VisualListItemFormattingEventHandler(ListElement_VisualItemFormatting);
            radTaskList.ListElement.Font = new Font("Microsoft Sans Serif", 16);
            radProjList.DropDownListElement.AutoCompleteSuggest.DropDownList.Popup.Font = new System.Drawing.Font("Microsoft Sans Serif", 26);
            radTaskList.EndUpdate();

            radTaskList.SelectedIndex = -1;
            radTaskList.Text = "Select Project Type";


        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void radProjList_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radProjList.SelectedIndex >= 0)
            {
                radTaskList.Select();
                PopulateTasks();
            }
        }
        private void radTaskList_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (radTaskList.SelectedIndex >= 0)
            {
                radTaskList.CloseDropDown();
                TextDisplay();

            }
        }
        Font myFont = new Font("Microsoft Sans Serif", 16);
        private void ListElement_VisualItemFormatting(object sender, Telerik.WinControls.UI.VisualItemFormattingEventArgs args)
        {
            args.VisualItem.Font = myFont;
        }

        private void radTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }


}

I have not added the code to InitializeComponents().

Any thoughts or help appreciated!


Solution

  • I would suggest unsubscribing from the SelectedIndexChanged event while populating the controls with data. You can subscribe again when everything is initialized. Here is the code:

    void PopulateProjects()
    {
        radProjList.SelectedIndexChanged -= radProjList_SelectedIndexChanged;
        radProjList.Visible = true;
        radTaskList.Visible = false;
        radProjList.Items.Clear();
        radProjList.Text = "Select Project";
        List<string> ProjectName = new List<string>();
        ProjectName.Add("abc");
        ProjectName.Add("def");
        ProjectName.Add("ghi");
        ProjectName.Add("jkl");
        ProjectName.Add("mno");
        ProjectName.Add("pqr");
    
    
        radProjList.DataSource = ProjectName;
        radProjList.DisplayMember = "ProjectName";
        radProjList.ValueMember = "ProjectName";
        radProjList.AutoCompleteDataSource = ProjectName;
    
        Size popupSize = new Size(400, 300);
    
        radProjList.DropDownListElement.DropDownMinSize = popupSize;
        radProjList.ListElement.Font = new Font("Microsoft Sans Serif", 16);
    
        radProjList.SelectedIndex = -1;
        radProjList.Text = "Select Project";
        radProjList.SelectedIndexChanged += radProjList_SelectedIndexChanged;
    
    }
    void PopulateTasks()
    {
        radTaskList.SelectedIndexChanged -= radTaskList_SelectedIndexChanged;
        List<string> populateTaskList = new List<string>();
        radTaskList.Visible = true;
        radTaskList.Items.Clear();
        populateTaskList.Add("task one");
        populateTaskList.Add("task two");
        populateTaskList.Add("task three");
        populateTaskList.Add("task four");
        populateTaskList.Add("task five");
        populateTaskList.Add("task six");
    
        radTaskList.Items.Clear();
        radTaskList.Text = "Select Tasks";
    
        radTaskList.DataSource = populateTaskList;
        radTaskList.DisplayMember = "projectTask";
        radTaskList.ValueMember = "projectTask";
        radTaskList.AutoCompleteDataSource = populateTaskList;
        radTaskList.AutoCompleteMode = AutoCompleteMode.Suggest;
        Size popupSize = new Size(400, 300);
        radTaskList.DropDownListElement.AutoCompleteSuggest.DropDownList.DropDownMinSize = popupSize;
        radTaskList.DropDownListElement.DropDownMinSize = popupSize;
        radTaskList.DropDownListElement.AutoCompleteSuggest.DropDownList.ListElement.VisualItemFormatting += new Telerik.WinControls.UI.VisualListItemFormattingEventHandler(ListElement_VisualItemFormatting);
        radTaskList.ListElement.Font = new Font("Microsoft Sans Serif", 16);
    
    
        radTaskList.SelectedIndex = -1;
        radTaskList.Text = "Select Project Type";
        radTaskList.SelectedIndexChanged += radTaskList_SelectedIndexChanged;
    
    }
    

    I hope this helps.