C# 實驗感悟WPF

實驗程序:todolistoop

本次博客主要記錄實驗程序開發過程。ui

 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格式,存在本地文件下。spa

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()

對應的,從程序中讀取與之綁定的本地數據。設計

前臺界面設計以下code

其中捕捉事件的button代碼以下blog

 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 若任何一項未填寫 則返回errorip

 

相對來講 前臺界面設計略簡便於後臺程序開發。開發

相關文章
相關標籤/搜索