我有兩個數組列表,其中我從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;
}
}
那麼究竟是什麼問題呢? – SpaceghostAli 2013-03-13 11:25:31
@SpaceghostAli那麼問題是其他值保存在模塊中,所以'代碼'''信息'''學期'等不會去第二個數組列表,當我點擊按鈕。 – user2157179 2013-03-13 11:32:48
好吧,你已經選擇了所有這些,你想要一點點移動它們?在這種情況下,你必須使用SelectedItems屬性,這是一個集合,而不是SelectedItem屬性,它只是一個單獨的對象。 – SpaceghostAli 2013-03-13 11:46:47