开发者

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

I found some wrong sequence in event life cycle in windows application in c#. I have 3 comboboxes in my windows form.

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:

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. but when assigning the datasource to the cmbSubject then its cmbSubjects_SelectedIndexChanged Event get called. again assigning when assigning the DisplayMember , same cmbSubjects_SelectedIndexCh开发者_运维知识库anged event get calls.

if observe in cmbSubjects_SelectedIndexChanged event i am calling FillChapter() method. where i am filling cmbChapter combo box, where same happening with cmbChapter while assigning Datasource and Display member, cmbChapters_SelectedIndexChanged event get calls.

Where as in cmbChapters_SelectedIndexChanged event i am filling Topics in cmbTopic combo box. it has no index changed event else it must called when assigning Data source and Display members to 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.


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. At the moment, your code assumes that SelectedItem will be non-null and that is not guaranteed. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜