Reputation: 755
I have a function, InsertItems:
public void InsertItems()
{
todoitemList.Clear();
todoSelect.Items.Clear();
foreach (XmlNode xmlNode in xmlDoc.SelectNodes("ToDoList/ToDo"))
{
ToDoItem item = new ToDoItem();
item.ID = xmlNode.SelectSingleNode("ID").InnerText;
item.Title = xmlNode.SelectSingleNode("Title").InnerText;
item.Description = xmlNode.SelectSingleNode("Desc").InnerText;
item.PriorityLevel = xmlNode.SelectSingleNode("Priority").InnerText;
item.Date = Convert.ToDateTime(xmlNode.SelectSingleNode("Date").InnerText);
item.TimeHour = Convert.ToInt32(xmlNode.SelectSingleNode("TimeHour").InnerText);
item.TimeMinute = Convert.ToInt32(xmlNode.SelectSingleNode("TimeMinute").InnerText);
item.TimeSecond = Convert.ToInt32(xmlNode.SelectSingleNode("TimeSecond").InnerText);
item.Completed = xmlNode.SelectSingleNode("Completed").InnerText;
todoitemList.Add(item);
todoSelect.Items.Add(item.Title);
todoIDList.Add(item.ID);
}
}
This function clears both a list and the combo box to which is used to select the items and then fills the list with the relevant data. ToDoItem is a class which contains properties - ID, Title and so forth.
When the function is executed within Form1.cs, it fully works as expected, clearing the list and adding the new data. However, when the function is executed within Form2.cs (main.InsertItems()), the foreach loop appears to be never ran and I've really no idea what is causing this.
Any help is much appreciated!
--
Edit:
main.InsertItems() is called in the following function:
private void createNew_Click(object sender, EventArgs e)
{
if (CheckAll())
{
XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ToDo", null);
XmlNode xmlNodeID = xmlDoc.CreateElement("ID");
xmlNodeID.InnerText = CreateRandomID();
XmlNode xmlNodeTitle = xmlDoc.CreateElement("Title");
xmlNodeTitle.InnerText = textBoxTitle.Text;
XmlNode xmlNodeDesc = xmlDoc.CreateElement("Desc");
xmlNodeDesc.InnerText = textBoxDesc.Text;
XmlNode xmlNodePriority = xmlDoc.CreateElement("Priority");
xmlNodePriority.InnerText = Convert.ToString(priorityLevel.SelectedItem);
XmlNode xmlNodeDate = xmlDoc.CreateElement("Date");
string currentDate = Convert.ToString(monthCalendar.SelectionRange.Start);
string strippedDate = currentDate.Substring(0, currentDate.Length - 8);
strippedDate += timeHour.Text + ":" + timeMinute.Text + ":" + timeSecond.Text;
xmlNodeDate.InnerText = strippedDate;
XmlNode xmlNodeTimeHour = xmlDoc.CreateElement("TimeHour");
xmlNodeTimeHour.InnerText = timeHour.Text;
XmlNode xmlNodeTimeMinute = xmlDoc.CreateElement("TimeMinute");
xmlNodeTimeMinute.InnerText = timeMinute.Text;
XmlNode xmlNodeTimeSecond = xmlDoc.CreateElement("TimeSecond");
xmlNodeTimeSecond.InnerText = timeSecond.Text;
XmlNode xmlNodeCompleted = xmlDoc.CreateElement("Completed");
xmlNodeCompleted.InnerText = "False";
xmlNode.AppendChild(xmlNodeID);
xmlNode.AppendChild(xmlNodeTitle);
xmlNode.AppendChild(xmlNodeDesc);
xmlNode.AppendChild(xmlNodePriority);
xmlNode.AppendChild(xmlNodeDate);
xmlNode.AppendChild(xmlNodeTimeHour);
xmlNode.AppendChild(xmlNodeTimeMinute);
xmlNode.AppendChild(xmlNodeTimeSecond);
xmlNode.AppendChild(xmlNodeCompleted);
xmlDoc.DocumentElement.AppendChild(xmlNode);
try
{
xmlDoc.Save(_fileName);
MessageBox.Show("Item successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
main.InsertItems();
}
catch (XmlException)
{
MessageBox.Show("Error! The item could not be added due to an XML error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (IOException)
{
MessageBox.Show("Error! The file could not be found or written to. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception)
{
MessageBox.Show("Error! An unknown error occured. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
}
}
CheckAll returns a boolean -> true if all fields are valid.
_fileName variable is correct and is saving to the correct file.
Upvotes: 1
Views: 182
Reputation: 27339
The problem is that you're not reloading xmlDoc
in Form1.InsertItems
.
In the code you posted, before calling Form1.InsertItems
from Form2
, you write the file to disk, but since you're not passing a reference to the updated xmlDoc
from Form2
on to Form1
, you don't see the changes show up on Form1
. InsertItems
works as expected when you call it from Form1
because xmlDoc
is a member variable for Form1
, so the changes are available in InsertItems
when called from Form1
.
Try either reloading the XmlDocument
from the file system at the beginning of InsertItems
or pass an XmlDocument
to InsertItems
as a parameter.
Upvotes: 2
Reputation: 67065
Is the other class running on another thread? This could be due to a cross-thread exception. Basically, there is only one UI thread to update UI elements. If you try to update UI elements from another thread, then you will get an exception. It is possible that you are just swallowing that error. You can try debugging it and you will see the exception before it is eaten. If that is indeed the case, then you will need to Invoke back onto the UI thread to perform the update.
Here is a good article on winforms cross thread UI updates.
Here is a rough example of what your code would look like (it is not compiler checked, but should be close :))
if ( main.InvokeRequired ) {
main.Invoke(new MethodInvoker(main.InsertItems));
} else {
main.InsertItems();
}
Upvotes: 0