简体   繁体   English

如果combobox1和combobox2为true,则更新combobox3中的项目

[英]If combobox1 and combobox2 is true then update the items in combobox3

I want to add new items/change the items in cmbSection if cmbCourse, cmbYear and cmbSems met the requirements. 如果cmbCourse,cmbYear和cmbSems满足要求,我想添加新项目/更改cmbSection中的项目。 I try this code but cmbSection won't add new items/change the items. 我尝试使用此代码,但cmbSection不会添加新项目/更改项目。

private void AddNewStudent_Load(object sender, EventArgs e)
    {
        if (cmbCourse.SelectedItem.Equals("BSIT"))
        {
            if (cmbYear.SelectedItem.Equals("1st"))
            {
                if (cmbSems.SelectedItem.Equals("1st"))
                {
                    cmbSection.Items.Add("IT101C");
                    cmbSection.Items.Add("IT102C");
                    cmbSection.Items.Add("IT103C");
                    cmbSection.Items.Add("IT104C");
                }
                else if (cmbSems.SelectedItem.Equals("2nd"))
                {
                    cmbSection.Items.Add("IT201C");
                    cmbSection.Items.Add("IT202C");
                    cmbSection.Items.Add("IT203C");
                    cmbSection.Items.Add("IT204C");
                }
            }
            else if (cmbYear.SelectedItem.Equals("2nd"))
            {
                if (cmbSems.SelectedItem.Equals("1st"))
                {
                    cmbSection.Items.Add("IT301C");
                    cmbSection.Items.Add("IT302C");
                    cmbSection.Items.Add("IT303C");
                    cmbSection.Items.Add("IT304C");
                }
                else if (cmbSems.SelectedItem.Equals("2nd"))
                {
                    cmbSection.Items.Add("IT401C");
                    cmbSection.Items.Add("IT402C");
                    cmbSection.Items.Add("IT403C");
                    cmbSection.Items.Add("IT404C");
                }
            }
        }
    }

I think you should use event in C#. 我认为您应该在C#中使用事件。 You register event SelectedIndexChanged of cmbCourse ==> When cmbCourse changed selected index, you will load new data into cmbYear. 您注册cmbCourse的事件SelectedIndexChanged ==>当cmbCourse更改了所选索引时,您会将新数据加载到cmbYear中。
You register event SelectedIndexChanged of cmbYear==> When cmbYear changed selected index, you will load new data into cmbSems. 您注册cmbYear ==>的事件SelectedIndexChanged事件。当cmbYear更改所选索引时,您会将新数据加载到cmbSems中。

You register event SelectedIndexChanged of cmbSems==> When cmbSems changed selected index, you will load new data into cmbSection. 您注册cmbSems的事件SelectedIndexChanged ==>当cmbSems更改了所选索引时,您会将新数据加载到cmbSection中。

You are checking all the conditions when the form loads; 表单加载时,您正在检查所有条件。 I assume you want to check it when the user changes the option in one of the combo boxes, in which case, as tauitdnmd stated, SelectedIndexChanged is the event you should be using, not Form.Load . 我假设您要在用户更改组合框之一中的选项时进行检查,在这种情况下,如tauitdnmd所述, SelectedIndexChanged是您应该使用的事件,而不是Form.Load You can set the SelectedIndexChanged event for each ComboxBox to all be handled by a single method, ie, the one you have (although I strongly recommend a name change). 您可以将每个ComboxBox的SelectedIndexChanged事件设置为全部由一个方法(即您拥有的方法)处理(尽管我强烈建议您更改名称)。

However, if you were to simply change which event is being handled, the use of Items.Add would result in the new options being added along side the old ones, quickly creating an enormously long and redundant drop-down. 但是,如果您只是要更改要处理的事件,则使用Items.Add会导致新选项与旧选项一起添加,从而迅速创建了一个冗长且冗长的下拉菜单。 The solution to this is simply clearing each ComboBox in question before adding all the relevant options with Items.Clear() . 解决方案是简单地清除每个有问题的ComboBox,然后再使用Items.Clear()添加所有相关选项。

@Vulcronos: I created a test application with the same code, only with a button click as the event, and the SelectedItem.Equals() check appears to works fine (the validity of its use notwithstanding). @Vulcronos:我使用相同的代码创建了一个测试应用程序,只需要单击按钮作为事件,并且SelectedItem.Equals()检查似乎可以正常工作(尽管其使用有效)。

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

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