/** * CreateContactDirectoryWindow implements creating a new form. This is the real 'constructor'. * First, we check if the fileName matches any of the Open Forms in the MultiSDIApplication. If it * does, we just activate that form. If it doesn't we create a new one using OpenFile, a method found * in this class (ContactDIrectoryForm) to open a file, then we activate the new window. */ public static ContactDIrectoryForm CreateContactDirectoryWindow(string fileName) { // If fileName is not empty if (!string.IsNullOrEmpty(fileName)) { // Loop through the open forms in MultiSDIApplication foreach (ContactDIrectoryForm openForm in Application.OpenForms) { // If the file we're trying to open is already open, i.e. file names match if (string.Compare(openForm.fileName, fileName, true) == 0) { // Bring form to top openForm.Activate(); return(openForm); } } } // Otherwise, create a new one with the given fileName ContactDIrectoryForm form = new ContactDIrectoryForm(); form.OpenFile(fileName); form.Show(); // Bring form to top form.Activate(); return(form); }
//To load contacts private void loadMenuItem_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { // if (dlg.FileName == this.Text) { return; } dlg.Filter = "Contact List Files|*.clf"; int currentLoadingContact = 0; if (dlg.ShowDialog() != DialogResult.OK) { return; } using (Stream stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read)) { IFormatter formatter = new BinaryFormatter(); Contact[] contactList = (Contact[])formatter.Deserialize(stream); ContactDIrectoryForm form = CreateContactDirectoryWindow(dlg.FileName); form.Text = dlg.FileName; //Check first to see if DataBindingSource count 0, as in the file is not open if (form.DataBindingSource.Count == 0) { while (currentLoadingContact < contactList.Length) { form.DataBindingSource.Add(contactList[currentLoadingContact]); currentLoadingContact++; } } } } }
/** * CreateContactDirectoryWindow helper method. Parses the command line args and grabs the file name, then passes the file * name to ContactDirectoryForm's CreateContactDirectoryWindow method to create a Top Level Window with the given file name. */ ContactDIrectoryForm CreateContactDirectoryWindow(ReadOnlyCollection <string> args) { // Get file name, if provided string fileName = (args.Count > 0 ? args[0] : null); // Create new top level form return(ContactDIrectoryForm.CreateContactDirectoryWindow(fileName)); }