Click here to Skip to main content
15,899,126 members
Home / Discussions / C#
   

C#

 
GeneralRe: Speed of execution Pin
AspDotNetDev16-Sep-11 10:38
protectorAspDotNetDev16-Sep-11 10:38 
AnswerRe: Speed of execution Pin
Eddy Vluggen16-Sep-11 11:03
professionalEddy Vluggen16-Sep-11 11:03 
GeneralRe: Speed of execution Pin
AspDotNetDev16-Sep-11 11:20
protectorAspDotNetDev16-Sep-11 11:20 
AnswerRe: Speed of execution Pin
Eddy Vluggen16-Sep-11 11:33
professionalEddy Vluggen16-Sep-11 11:33 
NewsRe: Speed of execution Pin
Eddy Vluggen16-Sep-11 13:44
professionalEddy Vluggen16-Sep-11 13:44 
QuestionUndo functionality for Node rename in TreeView Pin
Nyanoba16-Sep-11 1:28
Nyanoba16-Sep-11 1:28 
AnswerRe: Undo functionality for Node rename in TreeView Pin
BillWoodruff16-Sep-11 2:26
professionalBillWoodruff16-Sep-11 2:26 
AnswerRe: Undo functionality for Node rename in TreeView Pin
BobJanova16-Sep-11 3:51
BobJanova16-Sep-11 3:51 
You shouldn't 'implement the Undo Functionality for rename'. You should implement undo as an app-wide thing which maintains a stack of commands across the whole application and knows how to undo them.

There's two ways to do undo: either you maintain the state before each operation, or you store each operation as a command which knows how to undo itself. In all but the simplest case you want to do the second way. You need to implement every action within your application as a command, where command means something like:

interface IUndoableCommand {
 void Undo();
 void Redo();
}


In this case, assuming your tree view is bound to some view-model level property, you would hook the NodeRenamed event (whatever it's actually called) - pseudocode:
class ItemRenameCommand : IUndoableCommand {
 string oldname, newname;
 INamedModel Model { get; set; }
 
 ItemRenameCommand(INamedModel model, string oldname, string newname) {
  this.oldname = oldname; this.newname = newname;
  Model = model;
 }

 void Undo() { Model.Name = oldname; }
 void Redo() { Model.Name = newname; }
}

class MainForm : Form {
 ...
 void TreeNodeRenamed(object sender, NodeRenameEventArgs e){
  IUndoableCommand renameCommand = new ItemRenameCommand(treeViewModel, e.OldName, e.NewName);
  undoStack.Push(renameCommand);
 }

 Stack<IUndoableCommand> undoStack, redoStack;
}


(Remember, pseudocode; I can't remember how you actually hook onto the rename event of a TreeView, and it's not really the main point of the discussion, but it means that won't compile.) INamedModel is just for the Name property, you'd typically have all your view model classes implement something like that in a strongly typed environment.

One can have an argument about whether the undo/redo stacks should be in the UI (i.e. the view) or the view-model, taking a MVVM approach to structure. I tend to feel that because it represents user actions, it is part of the view.

Then, to undo, you take items off the undo stack and call .Undo() on each of them in turn, pushing them onto the redo stack. And to redo, you take items off the redo stack, move them to the undo stack and call .Redo() on each one.

The tricky bit is making sure that everything can be represented by a reversible command. For some things (simplistic example, a Reset or Randomise button) you will have to store most or all of the previous state, but avoid doing that in all cases where it is possible to do so, as storing the whole state for each undo entry is a big waste of storage.
GeneralRe: Undo functionality for Node rename in TreeView Pin
BillWoodruff16-Sep-11 17:01
professionalBillWoodruff16-Sep-11 17:01 
GeneralRe: Undo functionality for Node rename in TreeView Pin
Nyanoba19-Sep-11 19:22
Nyanoba19-Sep-11 19:22 
QuestionShow a hidden console again? Pin
Don Rolando15-Sep-11 22:49
Don Rolando15-Sep-11 22:49 
AnswerRe: Show a hidden console again? Pin
V.15-Sep-11 23:16
professionalV.15-Sep-11 23:16 
GeneralRe: Show a hidden console again? Pin
Don Rolando15-Sep-11 23:18
Don Rolando15-Sep-11 23:18 
GeneralRe: Show a hidden console again? Pin
V.15-Sep-11 23:37
professionalV.15-Sep-11 23:37 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 0:23
Don Rolando16-Sep-11 0:23 
GeneralRe: Show a hidden console again? Pin
V.16-Sep-11 0:36
professionalV.16-Sep-11 0:36 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 0:47
Don Rolando16-Sep-11 0:47 
AnswerRe: Show a hidden console again? Pin
Alan N16-Sep-11 3:04
Alan N16-Sep-11 3:04 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 3:19
Don Rolando16-Sep-11 3:19 
AnswerRe: Show a hidden console again? Pin
PIEBALDconsult16-Sep-11 3:19
mvePIEBALDconsult16-Sep-11 3:19 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 3:22
Don Rolando16-Sep-11 3:22 
QuestionDataGridView throws Data Pin
beauroak15-Sep-11 15:15
beauroak15-Sep-11 15:15 
AnswerRe: DataGridView throws Data Pin
Luc Pattyn15-Sep-11 15:29
sitebuilderLuc Pattyn15-Sep-11 15:29 
Questionretrive gridview cell value Pin
jashimu15-Sep-11 9:55
jashimu15-Sep-11 9:55 
AnswerRe: retrive gridview cell value Pin
walterhevedeich15-Sep-11 15:56
professionalwalterhevedeich15-Sep-11 15:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.