c# - 从不同的 arraylist 索引添加到 arraylist

标签 c# winforms arraylist listbox

我有两个数组列表,其中一个从 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;
                }
            }

最佳答案

好的,您似乎需要将添加到 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());

关于c# - 从不同的 arraylist 索引添加到 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383817/

相关文章:

c# - 如何在 C# 中有效地检查来自 URL 的 401 响应?

C# 执行外部程序并捕获(流)输出

c# - Winforms WebBrowser 设置最大内容宽度

c# - 如何从PictureBox获取真实图像像素点x,y

c# - 使用 XtraScheduler 通过特定自定义字段值获取约会

Java 数组列表和对象传递

java - 打印ArrayList时出现冗余结果

c# - IIS 服务的 ASP.NET 应用程序在服务大约 5-10 次后挂起

java - 为什么将对象添加到 ArrayList 时出现此错误 - Java

c# - Excel Interop 获取单元格内文本的像素宽度