I am still having problems with Document.IsModified for my project. It doesn’t set this property to false when a user loads or saves a document. This functionality works great in the ProtoApp sample application and I can’t understand why this is not working for me since I based our project on the ProtoApp example.
Currently, I am manually setting IsModified to false when the user saves a document, but I can’t get it to work when I load documents. IsModified is being called somewhere else but since IsModified is not overridable, I am not able to see where or when it is being called from. Any ideas how I can resolve this issue??
Joy
Just define a GoDocument.Changed event handler, and set a breakpoint in there immediately after you have set GoDocument.IsModified to false.
Alright, I am still trying to figure out how to fix this problem in my program. To try to get a better idea of how the ProtoApp sample works I have placed the following code in the GraphDoc.vb class:
Protected Overrides Sub OnChanged(ByVal evt As Northwoods.Go.GoChangedEventArgs)
MyBase.OnChanged(evt)
Console.WriteLine(DateTime.Now().ToLongTimeString() & " OnChanged called IsModified=" & MyBase.IsModified)
End Sub
Then I place the following code in the “Load” Function in the GraphDoc.vb class immediately after “AddDocument(loc, doc)”:
Console.WriteLine("after AddDocument ismodified = " & doc.IsModified)
The resulting lines that are written to the console when I run the ProtoApp sample are as follows:
after AddDocument ismodified = False
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=True
1:45:03 PM OnChanged called IsModified=False
So basically after I load a file, the OnChanged event is called 8 times. The first 7 times it sets the IsModified property to true even though it was set to false when I loaded the document. But magically in the 8th call IsModified is set back to False.
So my question is, what is going on here? What methods are overseeing this process and setting IsModified to true seven times but then setting it back to false again on the 8th call to the OnChanged event??
Joy
Well, that depends on your application. If you set a breakpoint where you are calling WriteLine, you can look at the stack to see what’s going on. You could also write out the value of evt.ToString(), to display additional information about the document change.
In this case I was referring to the ProtoApp sample application - everything I described in my last post I added to the ProtoApp sample application.
I know that there is no way for you to know what I am doing in my application, which is why I am trying to understand how the ProtoApp sample application works so that I can apply it to mine.
So again, can you tell me what is happening with the ProtoApp sample application and what calls the OnChanged event 8 times after a document is loaded?
Thanks
Joy
When the user does a File Open, the GraphViewWindow.Open shared function does the following basic actions:
- present an OpenFileDialog
- call GraphDoc.Load to create a new document and initialize it according to the contents of the file
- create a new GraphViewWindow
- replace the new GraphViewWindow’s View’s Document with the GraphDoc that was loaded
- show the GraphViewWindow
The step that is relevant to your question is when it creates a new GraphViewWindow. A GraphViewWindow is an MDI child Form. Constructing a GraphViewWindow will construct its child Controls, including a GraphView. Constructing a GraphView will automatically construct a GraphDoc, because every GoView had better have a GoDocument to hold objects that need to be displayed.
It is the execution of the GraphDoc constructor that is causing those document Changed events that you see. Try setting a breakpoint in GraphDoc’s Sub New(). Those are initialization changes being made to the standard document for a GraphView. But that document will be thrown away when it is replaced by the document that was constructed by loading the XML file.
You might ask: “Why create a document that you just throw away?”
The answer is that we wanted to fit in with the standard Windows Forms conventions regarding how Forms are created and initialized. When a GraphViewWindow is constructed we don’t know whether we are going to replace the GraphViewWindow’s GraphView’s GraphDoc or not. The standard InitializeComponent method that is defined by the Visual Studio .NET Form designer is not able to know about the possible presence of a GraphDoc that could be used to call an alternate GraphView constructor that would avoid creating a document that would just get thrown away.
Of course, GoView does have such an alternate constructor, exactly to avoid creating unnecessary GoDocuments, so implementing this would be very easy if we didn’t want to fit in with the conventions that Windows Forms and the VS.NET Form designer have established.
Anyway, in ProtoApp the GoDocument.IsModified flag is set to false at the end, so everything is the way it should be. In your application, if IsModified is getting set to true in the document that was Loaded, after being loaded, then you should see what is causing the change. Since breakpoints don’t distinguish between different instances of the same class, you’ll need to distinguish them manually, perhaps by looking at the Name or some other features.
thank you - that helps a lot and I will do as you suggested.
Very quickly though, I have been doing the same test in my application that I did in the ProtoApp sample application - and it looks like in my application the GoDocument.OnChanged gets called 2 extra times after the IsModified property is set to false. In these extra two times IsModified is being set back to true again. I’m trying to find out what is doing this but I haven’t been able to track it down yet. Do you have any ideas just off the top of your head of what might be calling the OnChanged sub two extra times?
I am still having difficulties with the IsModified property not being set to false when I save a graph in my interface. The only way I can get it to be set to false is to place the following line of code in my SaveAs() function:
Doc.FinishTransaction(“save”)
However, the problem with this is that now the user has the option to “undo” their save which I don’t want them to be able to do.
Do you have any other ideas how I can resolve this issue?
Joy
To answer your immediate question: you can clear out the undo manager by calling
Doc.UndoManager.Clear()
Or you can just delete the last transaction by calling
Doc.AbortTransaction()
instead of calling FinishTransaction.