Skip to main content

Wave Undo

To implement Undo, the simplest way is to use a MemoryStream object in your WaveDisplayForm (MDI Child) and simply serialize the Wave (which should be marked as [Serializable] to the stream and hold on to the stream object.

private MemoryStream _undoStream = new MemoryStream();
private bool _canUndo = false;

public void SaveForUndo()
{
     BinaryFormatter bf = new BinaryFormatter();
     bf.Serialize(_undoStream, wave);

     _undoStream.Flush();

     // This rewinds the stream so that when it is used for deserialization, it's ready to use,
    // otherwise, the deserialization will start deserializing from the end of the stream and would fail.
     _undoStream.Position = 0;
     _canUndo = true;
}
 
public void Undo()
{

     BinaryFormatter bf = new BinaryFormatter();
     _wave = (Wave)bf.Deserialize(_undoStream);
     _canUndo = false;

     // increment the modified counter on the wave (However you implemented this), i did it using the following method on the Wave object
     _wave.DecrementModificationCounter(); // simply does a _modifiedCounter--

     Invalidate();

}
    

Comments

  1. I thought the simpliest way to make
    private Wave _waveUndo = null;
    in WaveDisplayForm. If we need to make many Undo we can make a List (stack).
    Why we need these things with stream?
    Seems I don't understand something...

    ReplyDelete
  2. That would be fine too. The only reason I recommend to use a MemoryStream in general is to not have to copy each element within the Wave object in order to Clone the Wave object. In my long experience this has been always a cause of subtle bugs when someone adds a new data member to Wave and forgets to add the necessary changes for the Cloning method. So for this project, you are right, it's controlled, but in general I always advocate the MemoryStream approach.

    ReplyDelete
  3. Anonymous9:58 PM

    I'm getting an odd error when I try to implement the code. It throws a Serialization Excpetion that Type 'WaveViewer.WaveDisplayForm' in Assembly 'WaveViewer'is not marked as serializable. my Wave class is already marked as [Serializeable] For s&g's I marked WaveMgr and WaveDisplayForm as [Serializable]. Now it says System.Windows.Forms.Form is not marked as Serializable.
    Any suggestions on how to continue troubleshooting?

    ReplyDelete

Post a Comment

Popular posts from this blog

Track Files in Visual Studio

By default, Visual Studio’s Solution Explorer will update its selected item based on the currently active document. This is extremely annoying as it keeps expanding your projects and folders until your solution explorer becomes unmanageable and unusable, unless you collapse all Projects (Only available if you have Visual Studio Power Commands) installed) and even then, after collapsing the entire solution, the saga returns back. Turn off Active tracking 1. Turn your Active tracking off by going to Tools > Options 2. Select “Projects and Solutions” node 3. Uncheck the “Tack Active Item in Solution Explorer” Add a macro to do on-demand tracking 4. Tools > Marcos > Macro Explorer 5. You will see “MyMacros” Module (Node). Right mouse click on MyMacros and invoke “New Module…” 6. Name it anything you like. I Named it Utilities 7. Right mouse click on Utilizes and invoke “new Macro…” menu. The Macros IDE window opens. 8. Paste in the following into it within the Publ...

Google Instant search

Google Instant Search is the worst feature ever!!!. I could not for the life of me find a PERMANENT way to turn it off. Every time I turned it off using different means, it always came back. This is the only link I found useful so far, but the suggestion is not that great. It's OK. http://www.google.com/support/forum/p/Web%20Search/thread?tid=2a8bc9c3770bc11b&hl=en It is so annoying and so intrusive, every time I type a character it interrupts me and then I end up because of the time lag with a different string in the search box than the one I intended to type. Are they hiring a whole new set of STUPID generation at Google. This does not take a rocket scientist to figure out that this feature is useless. Even if they believe that is has some usefulness, let me (the customer, the user) decide. Anyone knows how to turn this stupid feature off?

Reduce Windows Folder Size

I just realized that my Windows directory has over 34 GB of stuff. What the heck!!! When you have Office, a couple of versions of Visual Studio, and few other Microsoft programs, you basically have already gobbled up about 80 GB of hard disk space. I found this online and found it to be useful and effective. It shaves off about 8 GB of your Windows folder. Still not what I had expected but I can definitely use 8 GB for other important documents. I'm just puzzled at what is Micros soft putting in an Operating system folder? huh? 28 GB of crap? Open a command prompt (make sure you either have Admin privileges). If you do not have Admin privileges, then open the command prompt as Admin. dism /online /cleanup-image /spsuperseded /hidesp