2013-03-13 96 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