从不同的ArrayList索引添加到一个ArrayList

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;
                }
            }

那么问题到底是什么? - HasaniH
@SpaceghostAli 好吧,问题在于其他保存在模块中的值,因此当我单击按钮时,“code”、“info”、“semester”等不会进入第二个数组列表。 - user2157179
好的,所以你已经选择了它们,想要一次性移动它们?在这种情况下,你需要使用SelectedItems属性,它是一个集合而不是SelectedItem属性,后者只是一个单独的对象。 - HasaniH
@SpaceghostAli 不,我只想将列表框中的一个值移动到另一个列表框中,当我这样做时,我希望将与所选移动项相关联的所有属性标签(如“id”、“code”等)移动到第二个数组列表中。 - user2157179
我猜你是指选择的ArrayList?但是,您在按钮事件处理程序中没有任何代码尝试从modules ArrayList读取数据并将其移动到另一个列表中,因此自然不会移动任何内容。 - HasaniH
@SpaceghostAli 是的,我指的是选定的 ArrayList。很遗憾,我没有任何代码,因为我尝试了以下代码但它不起作用:chosen.Add(module.Get(index)); - user2157179
1个回答

0

好的,看起来你需要将你添加到selectionBox的m.mName与你添加到module 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());

似乎不起作用。我尝试通过添加控制台输出语句进行实验,并尝试获取存储在列表中的值并将它们添加到XML中。 - user2157179
具体是在哪里出了问题?你是否收到了异常,或者出现了“找不到模块”的情况? - HasaniH
当我点击提交按钮时,它会失败,然后在尝试写入XML时,条件会得到空值。我将在上面添加代码。 - user2157179
你确认在创建模块实例时,mPreReq和所有其他成员都已设置吗?你需要进行调试并获取更多关于哪些部分出了问题的具体信息。 - HasaniH
是的,我确定,因为如果我在foreach中使用ArrayList模块,那么会出现错误标志,并且会提示存在req错误。 - user2157179

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接