C# 实验感悟WPF

实验程序:todolist

本次博客主要记录实验程序开发过程。

 1 public void Save()
 2         {
 3             System.Text.StringBuilder report = new System.Text.StringBuilder();
 4             foreach (Task toDoItem in taskList)
 5             {
 6                 report.Append(toDoItem.TaskTitle.ToString() + "\t" + toDoItem.TaskDescription.ToString() + "\t" + toDoItem.TaskDueDate.ToString() + "\t" + toDoItem.TaskPriority.ToString() + "\t" + toDoItem.CreationDate.ToString() + "\t" + toDoItem.IsTaskComplete.ToString() + "\n");
 7             }
 8 
 9             Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
10             dlg.FileName = "ToDoList"; // Default file name
11             dlg.DefaultExt = ".txt"; // Default file extension
12             dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
13 
14             // Show save file dialog box
15             Nullable<bool> result = dlg.ShowDialog();
16 
17             // Process save file dialog box results
18             if (result == true)
19             {
20                 // Save document
21                 File.WriteAllText(dlg.FileName, report.ToString());
22             }
23         }//End Save()

上述代码主要实现将数据保存成TXT格式,存在本地文件下。

public void Load()
        {
            taskList.Clear();

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "ToDoList"; // Default file name
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                //Open document 
                System.IO.StreamReader myFile = new System.IO.StreamReader(@dlg.FileName);
                int lineCount = 0;
                using (var reader = File.OpenText(@dlg.FileName))
                {
                    while (reader.ReadLine() != null)
                    {
                        lineCount++;
                    }
                }

                for (int i = 1; i <= lineCount; i++)
                {
                    string myString = myFile.ReadLine();
                    string[] obj = myString.Split('\t');
                    taskList.Add(new Task { TaskTitle = obj[0].ToString(), TaskDescription = obj[1].ToString(), TaskDueDate = obj[2].ToString(), TaskPriority = obj[3].ToString(), CreationDate = obj[4], IsTaskComplete = Convert.ToBoolean(obj[5]) });
                }//End for loop

            }//End statement

        } // end public bool Load()

对应的,从程序中读取与之绑定的本地数据。

前台界面设计如下

其中捕捉事件的button代码如下

 1  private void addTask_Click(object sender, RoutedEventArgs e)
 2         {
 3             if (!taskTitleTextBox.Text.Equals(string.Empty) && !descriptionTextBox.Text.Equals(string.Empty) && !dateField.Text.Equals(string.Empty) && !priorityDropBox.Text.Equals(string.Empty))
 4             {
 5                 toDo.addItem(taskTitleTextBox.Text, descriptionTextBox.Text, dateField.Text, priorityDropBox.Text);
 6                 toDoListView.Items.Refresh();
 7                 ChangeUIState(UIState.AddState);
 8                 isProgramSaved = false;
 9             }
10             else
11             {
12                 MessageBox.Show("Make sure all fields are filled in!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
13             }
14         }

其中包括对于任务时间 优先级 题目 及描述的填写判定

若全部填写则将数据添加进TEXT 若任何一项未填写 则返回error

 

相对来说 前台界面设计略简便于后台程序开发。

posted @ 2015-05-18 12:40  燃烧灬  阅读(594)  评论(0编辑  收藏  举报