功能未按预期执行

时间:2012-03-18 04:34:01

标签: c# winforms function foreach

我有一个函数,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);
        }
    }

此功能清除列表和用于选择项目的组合框,然后使用相关数据填充列表。 ToDoItem是一个包含属性的类--ID,Title等。

当在Form1.cs中执行该函数时,它完全按预期工作,清除列表并添加新数据。但是,当函数在Form2.cs(main.InsertItems())中执行时,foreach循环似乎永远不会运行,我真的不知道是什么导致了这个。

非常感谢任何帮助!

-

编辑:

main.InsertItems()在以下函数中调用:

    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返回一个布尔值 - >如果所有字段都有效,则为true。

_fileName变量是正确的,并保存到正确的文件中。

2 个答案:

答案 0 :(得分:2)

问题是您没有在xmlDoc重新加载Form1.InsertItems

在您发布的代码中,在从Form1.InsertItems调用Form2之前,您将该文件写入磁盘,但由于您没有从{{{}传递对更新的xmlDoc的引用1}}到Form2,您看不到Form1上显示的更改。当您从Form1调用InsertItems时,Form1会按预期工作,因为xmlDocForm1的成员变量,因此当InsertItems调用Form1时,这些更改可用{1}}。

尝试从XmlDocument开头的文件系统重新加载InsertItems,或将XmlDocument作为参数传递给InsertItems

答案 1 :(得分:0)

另一个类是否在另一个线程上运行?这可能是由于跨线程异常造成的。基本上,只有一个UI线程可以更新UI元素。如果您尝试从另一个线程更新UI元素,那么您将获得异常。您可能只是吞下了这个错误。您可以尝试调试它,在吃之前您将看到异常。如果确实如此,那么您需要调用UI线程来执行更新。

Here is a good article on winforms cross thread UI updates.

以下是您的代码看起来像的一个粗略示例(它不是编译器检查,但应该关闭:))

if ( main.InvokeRequired ) {
    main.Invoke(new MethodInvoker(main.InsertItems));
} else {
    main.InsertItems();
}
相关问题