简体   繁体   English

在点网(C#)中处理Windows应用程序中的组合框事件

[英]handle combobox events in windows application in dot net (c#)

I found some wrong sequence in event life cycle in windows application in c#. 我在c#中的Windows应用程序中发现了事件生命周期中的一些错误序列。 I have 3 comboboxes in my windows form. 我的Windows窗体中有3个组合框。

On Form load filling first combobox as 在表单加载中,将第一个组合框填充为

 private void Form1_Load(object sender, EventArgs e)
        {
          FillSubjects();
        }
         public void FillSubjects()
        {
            List<Subject> Subjects = objEntityManager.GetSubjects();
            if (Subjects.Count != 0)
            {   cmbSubjects.DataSource = Subjects;
                cmbSubjects.DisplayMember = "SubjectName";
                cmbSubjects.ValueMember = "SubjectId";
                CommonDataObject.SelectedSubject =    

              Convert.ToInt32(cmbSubjects.SelectedValue);
            } 
       }
        else
        {
            cmbSubjects.DataSource = null;

            cmbSubjects.Enabled = false;

        }

on changed of cmbsubject combo box filling Chapter combo box: 关于cmbsubject组合框填充章节组合框的更改:

private void cmbSubjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillChapters();
        }
  public void FillChapters()
        {           
            int subjectId = (cmbSubjects.SelectedItem as Subject).SubjectId;
            if (subjectId > 0)
            {
                List<Chapter> Chapters = new List<Chapter>();
                Chapters = objEntityManager.GetChapters(subjectId);
                if (Chapters.Count != 0)
                {

                    cmbChapters.DataSource = Chapters;
                    cmbChapters.DisplayMember = "ChapterName";
                    cmbChapters.ValueMember = "ChapterId";
                    CommonDataObject.SelectedChapter = 
                    Convert.ToInt32(cmbChapters.SelectedValue);
                }
                else
                {
                    cmbChapters.DataSource = null;
                    cmbChapters.Enabled = false;

                }
            }

        }  }

where as on Chapters Selected index changed event filling 如上章节中所选索引更改了事件填充

private void cmbChapters_SelectedIndexChanged(object sender, EventArgs e)
        {
           FillTopics();
        }

 public void FillTopics()
        {           

            int chpterId = (cmbChapters.SelectedItem as Chapter).ChapterId;
            if (chpterId > 0)
            {
                List<Topic> Topics = objEntityManager.GetTopics(chpterId);
                if (Topics.Count != 0)
                {
                    cmbtopics.DataSource = objEntityManager.GetTopics(chpterId);
                    cmbtopics.DisplayMember = "TopicName";
                    cmbtopics.ValueMember = "TopicId";
                    CommonDataObject.SelectedTopic =                         
                    Convert.ToInt32(cmbtopics.SelectedValue);
                }
                else
                {
                    cmbtopics.DataSource = null;
                    cmbtopics.Enabled = false;
                }
            }
        } 

Now problem is, 现在的问题是

when form load event get raised obviously Fillsubject() Method get called as per code where cmbSubjects get fills with objects of subject in List. 当明显引发表单加载事件时,按代码调用Fillsubject()方法,其中cmbSubjects用List中的subject对象填充。 but when assigning the datasource to the cmbSubject then its cmbSubjects_SelectedIndexChanged Event get called. 但是将数据源分配给cmbSubject时,将调用其cmbSubjects_SelectedIndexChanged事件。 again assigning when assigning the DisplayMember , same cmbSubjects_SelectedIndexChanged event get calls. 在分配DisplayMember时再次分配,相同的cmbSubjects_SelectedIndexChanged事件获取调用。

if observe in cmbSubjects_SelectedIndexChanged event i am calling FillChapter() method. 如果在cmbSubjects_SelectedIndexChanged事件中观察到,我正在调用FillChapter()方法。 where i am filling cmbChapter combo box, where same happening with cmbChapter while assigning Datasource and Display member, cmbChapters_SelectedIndexChanged event get calls. 在我填充cmbChapter组合框的地方,在分配数据源和Display成员时,与cmbChapter发生的情况相同,cmbChapters_SelectedIndexChanged事件获取调用。

Where as in cmbChapters_SelectedIndexChanged event i am filling Topics in cmbTopic combo box. 在cmbChapters_SelectedIndexChanged事件中,我在cmbTopic组合框中填充主题。 it has no index changed event else it must called when assigning Data source and Display members to cmbTopics. 它没有索引更改事件,否则在将数据源和显示成员分配给cmbTopics时必须调用它。

in this way number of time events are calling again and again unnecessarily. 通过这种方式,不必要地一次又一次地调用了许多时间事件。 hao to avoid this ? 好避免这种情况? and why should this happening ? 为什么会这样呢? i am new in windows application development so please guide me. 我是Windows应用程序开发的新手,请指导我。

If something changes the contents of a combo box, it also changes the item that is selected, because the items available to be selected from has changed. 如果某项更改了组合框的内容,它也会更改所选的项目,因为可从中选择的项目已更改。 This is expected behaviour. 这是预期的行为。

When responding to the SelectedIndexChanged event, you should be aware that there may be nothing selected and act accordingly. 响应SelectedIndexChanged事件时,应注意可能没有选择任何内容,因此应采取相应的措施。 At the moment, your code assumes that SelectedItem will be non-null and that is not guaranteed. 目前,您的代码假定SelectedItem将为非null,并且不能保证。 You should also check to see if the new selection is different from the previous selection as there's no point in repopulating a list if it is going to contain the same things. 您还应该检查新选择是否与先前选择不同,因为如果列表包含相同内容,则没有必要重新填充列表。

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

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