Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot change labels using listbox selected item via class
    text
    copied!<p>I have a listbox which holds a name of a tag from and xml file, there are other tags which are also held in a class but when I try to click on a value inside the listbox to set my label text to what the value is inside of the class I get the following error: "InvalidCastException was unhandled. Unable to cast object of type 'System.String' to type 'Coursework1.modules'." Heres the code, this is inside the listbox event for when the index has changed.</p> <pre><code>for (int i = 0; i &lt; 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; } } </code></pre> <p>//Selection box creation</p> <pre><code> 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(); </code></pre> <p>//Label text being used</p> <pre><code>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(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload