タグの名前とxmlファイルを保持するリストボックスがあります。クラスにも保持されている他のタグがありますが、リストボックス内の値をクリックしてラベルテキストを値の内容に設定しようとすると「InvalidCastException が処理されませんでした。型 'System.String' のオブジェクトを型 'Coursework1.modules' にキャストできません。」というエラーが表示されます。コードは次のとおりです。これは、インデックスが変更されたときのリストボックス イベント内にあります。
for (int i = 0; i < selectionBox.Items.Count; i++)
{
if (selectionBox.GetSelected(i) == true)
{
infoLabel.Text = ((modules)selectionBox.Items[i]).mInfo;
lectureSlotLabel.Text = ((modules)selectionBox.Items[i]).mLSlot;
tutorialLabel.Text = ((modules)selectionBox.Items[i]).mTSlot;
prerequisiteLabel.Text = ((modules)selectionBox.Items[i]).mPreReq;
codeLabel.Text = ((modules)selectionBox.Items[i]).mCode;
nameLabel.Text =((modules)selectionBox.Items[i]).mName;
}
}
//セレクションボックス作成
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")
//{
// module.Add(new modules(textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString()));
//}
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();
//使用中のラベルテキスト
var xmlFile = XDocument.Load(xmlDirectory + @"\XML.xml");
//Finds the XML file again and looks for the Name tag
var mName = from directory in xmlFile.Descendants("Name")
where directory.Value == (String)selectionBox.SelectedItem
select directory.Parent.Element("Name").Value;
//in the mName we search the directory for the Name tag and after we search
//for the selected item of the list in the directory after that we look for
//the "Name" element in the directory and that gets the name value and saves it to
//mName
foreach (var item in mName)
{
//use the loop to make it dynamic as when the selected item changes so does,
//the moduleName label
nameLabel.Text = item.ToString();
}