private void TeaRepositoryView_DragDrop(object sender, DragEventArgs e) { var location = TeaRepositoryView.PointToClient(new Point(e.X, e.Y)); int draggedIndex = TeaRepositoryView.IndexFromPoint(location); if (draggedIndex < 0 || draggedIndex == TeaRepositoryView.Items.Count - 1) { return; } var fromIndex = (int)e.Data.GetData(typeof(int)); if (fromIndex == draggedIndex) { return; } var item = TeaRepositoryView.Items[fromIndex]; if (item is IEntry <TeaTimerDefinition> ) { var entry = (IEntry <TeaTimerDefinition>)item; _setup.Repository.MoveTo(entry, draggedIndex); TeaRepositoryView.Items.Clear(); TeaRepositoryView.Items.AddRange(_setup.Repository.ToArray()); TeaRepositoryView.Items.Add("<New>"); TeaRepositoryView.SelectedIndex = draggedIndex; } }
private void SaveBtn_Click(object sender, EventArgs e) { var idx = TeaRepositoryView.SelectedIndex; var item = TeaRepositoryView.Items[idx]; var newDefinition = TeaTimerDefinition.Create(NameTextBox.Text, (int)MinuteUpDown.Value, (int)SecondUpDown.Value); if (newDefinition.IsError) { MessageBox.Show(this, ((Error)newDefinition).Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } TeaRepositoryView.BeginUpdate(); if (item is IEntry <TeaTimerDefinition> ) { var entry = (IEntry <TeaTimerDefinition>)item; entry.Value = newDefinition; _setup.Repository.Update(entry); TeaRepositoryView.Items[idx] = entry; } else { var entry = _setup.Repository.Add(newDefinition); TeaRepositoryView.Items.Insert(TeaRepositoryView.Items.Count - 1, entry); SaveBtn.Text = "Save"; } TeaRepositoryView.SelectedIndex = idx; TeaRepositoryView.EndUpdate(); }
private void TeaRepositoryView_DragOver(object sender, DragEventArgs e) { var location = TeaRepositoryView.PointToClient(new Point(e.X, e.Y)); int draggedIndex = TeaRepositoryView.IndexFromPoint(location); if (draggedIndex < 0 || draggedIndex == TeaRepositoryView.Items.Count - 1) { e.Effect = DragDropEffects.None; } else { e.Effect = DragDropEffects.Move; } }
private void TeaRepositoryView_MouseDown(object sender, MouseEventArgs e) { var pointedIndex = TeaRepositoryView.IndexFromPoint(e.Location); if (pointedIndex < 0 || pointedIndex > TeaRepositoryView.Items.Count - 2) { TeaRepositoryView.ContextMenu = null; pointedIndex = TeaRepositoryView.Items.Count - 1; } TeaRepositoryView.SelectedIndex = pointedIndex; UpdateFormFieldsFromRepositoryView(pointedIndex); if (pointedIndex != TeaRepositoryView.Items.Count - 1 && e.Button != MouseButtons.Right) { TeaRepositoryView.DoDragDrop(pointedIndex, DragDropEffects.Move); } }