public void OpenDocumentWithView() { string path = ""; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { path = openFileDialog.FileName; } else { return; } } string docName = System.IO.Path.GetFileName(path); SignalDocument openedDoc = new SignalDocument(docName); documents.Add(openedDoc); CreateView(openedDoc, true); openedDoc.LoadDocument(path); }
/// <summary> /// Megnyit egy dokumentumot. Nincs implementálva. /// </summary> public void OpenDocument() { // 1. Fájl útvonal megkérdezése a felhasználótól (OpenFileDialog). // http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx // Lényeges, hogy az MSDN-ben látható példával ellentétben NE nyissuk meg a fájlt // a dialóguson OpenFile hívásával, helyette egyszerűen a FileName property // segítségével kérdezzük le a felhasználó által megadott útvonalat (amennyiben // a felhasználó az OK-kal zárta be az ablakot). string filename = ""; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Get the path of specified file filename = openFileDialog.FileName; } } // 2. Új dokumentum objektum létrehozása, regisztrálása, nézet létrehozása, stb. // , a NewDocument művelet szolgálhat mintaként. // A dokumentum neve a fájl neve legyen a könyvtár nélkül (ehhez használja a // System.IO.Path osztály GetFileName statikus függvényét) Document doc = new SignalDocument(Path.GetFileName(filename)); documents.Add(doc); createView(doc, true); // 3. Dokumentumba adatok betöltése (LoadDocument hívása dokumentum objektumon) doc.LoadDocument(filename); // 4. Nézetek értesítése, hogy frissítsék magukat. // Megjegyzés. Erre alapvetően a dokumentum osztály UpdateAllViews művelete használandó. // Ez viszont protected , itt nem elérhető. Ne tegye publikussá! Helyette kövesse // az egységbezárás alapelvét: az előző, 3. lépésben meghívta a SignalDocument.LoadDocument // műveletét. Tegye az UpdateAllViews hívást a LoadDocument végére. A Document-View // architektúrának ez az egyik lényegi pontja: minden dokumentum módosító művelet végére // oda kell tenni az UpdateAllViews hívást, hogy a nézetek tükrözzék a változásokat. }
/// <summary> /// Opens a document. Not implemented yet. /// </summary> public void OpenDocument() { // 1. Get the file path from the user (OpenFileDialog) // http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx // As opposed to the example in the MSDN dont open the file with OpenFile. // Instead, just get the file path selected by the user with the FileName property // (only if the user pressed the OK button and not something else). OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.FilterIndex = 0; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) // after clicking OK in the Open Dialog we receive the file path(filename) { filename = openFileDialog.FileName; } ; // 2. Crate a new document, register it, create its first view, etc. // ... just like in the NewDocument method. // Use the filename as the name of the document (use the GetFileNam static method of System.IO.Path). // NewDocForm form = new NewDocForm(); SignalDocument doc = new SignalDocument(filename); //we create a new signal doc with the filename received documents.Add(doc); // add it to documents createView(doc, true); //make the new view // 3. Load the data to the document (call the LoadDocument method of the document). doc.LoadDocument(filename); //we load the document // 4. Notify the views to refresh themselves. // Note: the UpdateAllViews method of the document class is responsible for it. // Though, it is protected and you cannot access that from here. Dont make it public. // Follow the principle of encapsulation. In the previous step you called the // SignalDocument.LoadDocument method of the document. Call UpdateAllViews at the end of // the LoadDocument method. This is one of the crucial points of the Document-View // architecture that all the operations that modify the document should call the UpdateAllViews // methods to notify the views about the change (so that they can refresh themselves). }
public void OpenDocument() { OpenFileDialog od = new OpenFileDialog(); if (od.ShowDialog() != DialogResult.OK) { return; } string path = od.FileName; SignalDocument doc = new SignalDocument(System.IO.Path.GetFileName(path)); documents.Add(doc); createView(doc, true); doc.LoadDocument(path); }