gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:35:08 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com