2013-03-13 76 views
3

我有两个数组列表,其中我从XML中读取值,然后向列表框添加特定标记。从列表框中,我通过标签传输到另一个列表框,但是我遇到的问题是当试图获取array1中列表框中所选项目的值以移至array2时。 我该怎么做,并确保在arraylist1的当前索引中保存的所有内容都移动到arraylist2?从不同的列表索引添加到列表列表

//初始化

int moduleCount = 0; 
    bool isFull = false; 
    ArrayList chosen= new ArrayList(); 
    ArrayList module = new ArrayList(); 
    String name; 
    String code; 
    String info; 
    String semester; 
    String tSlot; 
    String lSlot; 
    String preReq; 
    string xmlDirectory = Directory.GetCurrentDirectory(); 
    public Form1() 
    { 
     InitializeComponent(); 
     createBox(); 
     //List<Array> a=new List<Array>();    

     // Console.WriteLine(module.ToString()); 
     // getXML(); 
    } 

    private void createBox() 
    { 
     String workingDir = Directory.GetCurrentDirectory(); 

     XmlTextReader textReader = new XmlTextReader(workingDir + @"\XML.xml"); 
     textReader.Read(); 
     XmlNodeType type; 

     while (textReader.Read()) 
     { 
      textReader.MoveToElement(); 
      type = textReader.NodeType; 
      if (type == XmlNodeType.Element) 
      { 
       if (textReader.Name == "Code") 
       { 
        textReader.Read(); 
        code = textReader.Value; 
        Console.WriteLine(code); 
       } 
       if (textReader.Name == "Name") 
       { 
        textReader.Read(); 
        name = textReader.Value; 
        //selectionBox.Items.Add(name); 
        Console.WriteLine(name); 
       } 
       if (textReader.Name == "Semester") 
       { 
        textReader.Read(); 
        semester = textReader.Value; 
        Console.WriteLine(semester); 
       } 
       if (textReader.Name == "Prerequisite") 
       { 
        textReader.Read(); 
        preReq = textReader.Value; 
        Console.WriteLine(code); 
       } 
       if (textReader.Name == "LectureSlot") 
       { 
        textReader.Read(); 
        lSlot = textReader.Value; 
        Console.WriteLine(lSlot); 
       } 
       if (textReader.Name == "TutorialSlot") 
       { 
        textReader.Read(); 
        tSlot = textReader.Value; 
        Console.WriteLine(tSlot); 
       } 
       if (textReader.Name == "Info") 
       { 
        textReader.Read(); 
        info = textReader.Value; 
        Console.WriteLine(info); 
        module.Add(new Modules(code, name, semester, tSlot, lSlot, info, preReq)); 
       } 
      } 

      //Console.WriteLine(module); 
     } 
     foreach (object o in module) 
     { 
      Modules m = (Modules)o; 
      //String hold = m.mName; 
      selectionBox.Items.Add(m.mName); 
     } 
     textReader.Close(); 

//按钮的事件处理程序从一个列表框移动到另一个

if (selectionBox.SelectedItem != null) 
     { 
      chosenBox.Items.Add(selectionBox.SelectedItem); 
      selectionBox.Items.Remove(selectionBox.SelectedItem); 
      chosen.Add(selectionBox.SelectedItem); 
      errorLabel.Text = ""; 
      moduleCount++; 
      if (moduleCount >= 8) 
      { 
       isFull = true; 
       errorLabel.Text = "You have selected 8 Modules please fill the fields and submit"; 
       selectionBox.Enabled = false; 
      } 
     } 
     else 
     { 
      errorLabel.Text = "Please select a module"; 
     } 

     numberChosen.Text = String.Format(moduleCount.ToString()); 

//写XML

foreach (object o in chosen) 
      { 
       Modules m = (Modules)o; 
       if (m.mPreReq != "None") 
       { 
        MessageBox.Show("You must chose module " + m.mPreReq); 
        //errorLabel.Text = "You must chose module " + m.mPreReq; 
        errorLabel.Text = "There is a prereq course"; 
        req = true; 
       } 
      } 
+0

那么究竟是什么问题呢? – SpaceghostAli 2013-03-13 11:25:31

+0

@SpaceghostAli那么问题是其他值保存在模块中,所以'代码'''信息'''学期'等不会去第二个数组列表,当我点击按钮。 – user2157179 2013-03-13 11:32:48

+0

好吧,你已经选择了所有这些,你想要一点点移动它们?在这种情况下,你必须使用SelectedItems属性,这是一个集合,而不是SelectedItem属性,它只是一个单独的对象。 – SpaceghostAli 2013-03-13 11:46:47

回答

0

好好像您需要将添加到selectionBox的m.mName与添加到模块ArrayList的实际Module关联起来,以便于您可以稍后将该模块的成员添加到所选的ArrayList。例如:

if (selectionBox.SelectedItem != null) 
{ 
    chosenBox.Items.Add(selectionBox.SelectedItem); 
    selectionBox.Items.Remove(selectionBox.SelectedItem); 

    foreach (Module m in module) 
    { 
     if (m.Name.Equals(selectionBox.SelectedItem) 
     { 
      chosen.Add(m.Info); 
      chosen.Add(m.Code); 
      ... 
      break; 
     } 
    } 

    errorLabel.Text = ""; 
    moduleCount++; 
    if (moduleCount >= 8) 
    { 
     isFull = true; 
     errorLabel.Text = "You have selected 8 Modules please fill the fields and submit"; 
     selectionBox.Enabled = false; 
    } 
    else 
    { 
     errorLabel.Text = "Please select a module"; 
    } 
} 

numberChosen.Text = String.Format(moduleCount.ToString()); 
+0

它似乎没有工作我试图通过添加consolewritelines试图获取存储在列表中的值,并将它们添加到XML – user2157179 2013-03-13 12:50:31

+0

特别是它失败?你有没有发现异常,有没有找到模块? – SpaceghostAli 2013-03-13 12:52:37

+0

当我按下提交,然后尝试写入XML时,它会失败,它会为条件获取空值。我将添加上面的代码。 – user2157179 2013-03-13 13:10:33