コード例 #1
0
        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                AddEditTodoDialog addEditDialog = new AddEditTodoDialog(null);
                addEditDialog.Owner = this;

                addEditDialog.AddNewTodoCallback += (t) => { currTodo = t; };
                bool?result = addEditDialog.ShowDialog();

                if (result == true)
                {
                    int newId = db.AddTodo(currTodo);
                    currTodo.Id = newId;
                    todoList.Add(currTodo);
                    lstView.ItemsSource = todoList;
                    lstView.Items.Refresh();
                }
                operation             = "Add new Todo";
                lstView.SelectedIndex = lstView.Items.Count - 1;//for status bar selection change
            }
            catch (DataInvalidException ex)
            {
                MessageBox.Show(ex.Message, "Error Information");
            }
        }
コード例 #2
0
        private void Update_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstView.SelectedItems.Count != 1)
            {
                MessageBox.Show("Please choose one item to update", "Information");
                return;
            }

            try
            {
                int index = lstView.SelectedIndex;
                AddEditTodoDialog addEditDialog = new AddEditTodoDialog((Todo)lstView.SelectedItem);
                addEditDialog.Owner = this;

                addEditDialog.AddNewTodoCallback += (c) => { currTodo = c; };
                bool?result = addEditDialog.ShowDialog();   // this line must be stay after the assignment, otherwise value is not assigned

                if (result == true)
                {
                    db.UpdateTodo(currTodo);
                    todoList.ElementAt(index).Id      = currTodo.Id;
                    todoList.ElementAt(index).Task    = currTodo.Task;
                    todoList.ElementAt(index).DueDate = currTodo.DueDate;
                    todoList.ElementAt(index).Status  = currTodo.Status;
                }
                lstView.Items.Refresh();
                operation             = "Update one record";
                lstView.SelectedIndex = 0;
            }
            catch (DataInvalidException ex)
            {
                MessageBox.Show(ex.Message, "Error Information");
            }
        }