private void dataListViewSceneCMDs_ModelDropped(object sender, ModelDropEventArgs e) { int TargetIndex = 0; //Handle if dropped into empty Action box if (e.TargetModel == null) TargetIndex = 0; else TargetIndex = e.DropTargetIndex; // Handle Device Drop if (e.SourceModels[0].GetType().Equals(typeof(device))) { scene scene = (scene)dataListViewScenes.SelectedObject; using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString)) { scene selected_scene = db.scenes.FirstOrDefault(c => c.id == scene.id); if (selected_scene != null) { if (selected_scene.is_running) { MessageBox.Show("Cannot modify scene when it is running.", ProgramName); return; } foreach (device selected_device in e.SourceModels) { if (selected_device != null) { int pos = TargetIndex; switch (e.DropTargetLocation) { case DropTargetLocation.BelowItem: pos += 1; break; } AddEditSceneDeviceCMD addCMDform = new AddEditSceneDeviceCMD(null, selected_device.id, selected_scene.id, pos); addCMDform.ShowDialog(); } } } } } else if (e.SourceModels[0].GetType().Equals(typeof(scene_commands))) { //Rearrage Actions scene scene = (scene)dataListViewScenes.SelectedObject; using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString)) { scene selected_scene = db.scenes.FirstOrDefault(c => c.id == scene.id); if (selected_scene != null) { if (selected_scene.is_running) { MessageBox.Show("Cannot modify scene when it is running.", ProgramName); return; } switch (e.DropTargetLocation) { case DropTargetLocation.AboveItem: //offset = 0 dataListViewSceneCMDs.MoveObjects(TargetIndex, e.SourceModels); dataListViewSceneCMDs.SelectedObjects = e.SourceModels; break; case DropTargetLocation.BelowItem: //offset = 1 dataListViewSceneCMDs.MoveObjects(TargetIndex + 1, e.SourceModels); dataListViewSceneCMDs.SelectedObjects = e.SourceModels; break; } //SAve the sort order to the DB foreach (scene_commands cmd in dataListViewSceneCMDs.Objects) { scene_commands scene_cmd = db.scene_commands.FirstOrDefault(s => s.id == cmd.id); if (scene_cmd != null) { scene_cmd.sort_order = dataListViewSceneCMDs.IndexOf(cmd); } db.SaveChanges(); } } } } }
private void editCommandToolStripMenuItem_Click(object sender, EventArgs e) { foreach (scene_commands cmd in dataListViewSceneCMDs.SelectedObjects) { using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString)) { scene_commands selected_sceneCMD = db.scene_commands.FirstOrDefault(c => c.id == cmd.id); if (selected_sceneCMD != null) { if (selected_sceneCMD.scene.is_running) { MessageBox.Show("Cannot modify scene when it is running.", ProgramName); return; } switch ((command_types)selected_sceneCMD.command_type_id) { case command_types.builtin: AddEditSceneBuiltinCMD b_cmd_editForm = new AddEditSceneBuiltinCMD(selected_sceneCMD.id); b_cmd_editForm.ShowDialog(); break; case command_types.device_command: case command_types.device_type_command: { AddEditSceneDeviceCMD editCMDform = new AddEditSceneDeviceCMD(selected_sceneCMD.id, selected_sceneCMD.device_id.Value); editCMDform.ShowDialog(); break; } } } } } }