3

I have two array lists one in which I read in values from an XML, and then I add a specific tag to a listbox. From the listbox I transfer over the tag to another listbox, but the problem I am having is when trying to get the values of the selected item in the listbox in array1 to move over to array2. How can I do this and make sure all things saved in the current index of arraylist1 move to arraylist2?

//Initialisation

    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();

//button event handler to move from one listbox to the other

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());

//Write to 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;
                }
            }
4

1 回答 1

0

好的,您似乎需要将添加到 selectionBox 的 m.mName 与您添加到模块 ArrayList 的实际模块相关联,以便稍后可以将该模块的成员添加到所选的 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());
于 2013-03-13T12:39:58.707 回答