简体   繁体   English

Telerik 下拉控件不允许我从下拉列表中选择 select 项目

[英]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).我创建了一个带有 4 个控件(一个按钮、2 个下拉列表和一个文本框)的 Telerik 测试应用程序。

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.我要做的是,在点击“添加项目”按钮时,会显示第一个下拉列表(来自 PopulateProjects() 方法的项目列表)。 On selecting a project from this list, a next drop-down list with "project tasks from PopulateTasks() method" gets displayed.从该列表中选择项目后,将显示下一个下拉列表,其中包含“来自 PopulateTasks() 方法的项目任务”。 On selecting a task, the text box control displays the text content of both the project and task selected(TextDisplay() method).在选择一个任务时,文本框控件显示项目和所选任务的文本内容(TextDisplay() 方法)。

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.我面临的问题是,下拉控件不会等我到 select 一个项目,而是默认自动选择列表中的第一个项目。 So the text box displays the first item in project list and project task ie it displays "PROJECT abc TASK task one".因此文本框显示项目列表和项目任务中的第一项,即显示“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.流程从 radProjList.EndUpdate 直接到 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().我还没有将代码添加到 InitializeComponents()。

Any thoughts or help appreciated!任何想法或帮助表示赞赏!

I would suggest unsubscribing from the SelectedIndexChanged event while populating the controls with data.我建议在使用数据填充控件时取消订阅 SelectedIndexChanged 事件。 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.我希望这有帮助。

I've not worked a lot with WinForms before, although I find your explicit call尽管我发现您的明确调用,但我以前没有经常使用 WinForms

radTaskList.Select(); 

a bit strange.有点奇怪。

I would suggest removing that and only handle the text of the selected item.我建议删除它并只处理所选项目的文本。 Have you tried debugging and adding a breakpoint in the entry of radProjList_SelectedIndexChanged method?您是否尝试过调试并在 radProjList_SelectedIndexChanged 方法的条目中添加断点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM