Multiple-SDI Application
-
I want my application to behave like MSWord 2003. i.e. Multiple-SDI Application. I am facing problem with the code that first window always close all other windows. Please identify changes.
//TopLevelForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace MultipleSDI { public partial class TopLevelForm : Form { string fileName; static int formCount = 0; public TopLevelForm() { InitializeComponent(); MultiSDIApplication.Application.AddTopLevelForm(this); MultiSDIApplication.Application.AddWindowMenu(this.windowToolStripMenuItem); ++formCount; this.Text += ": " + formCount.ToString(); } public static TopLevelForm CreateTopLevelWindow(string fileName) { // Detect whether file is already open if (!string.IsNullOrEmpty(fileName)) { foreach (TopLevelForm openForm in Application.OpenForms) { if (string.Compare(openForm.FileName, fileName, true) == 0) { // Bring form to top openForm.Activate(); return openForm; } } } // Create new top-level form and open file TopLevelForm form = new TopLevelForm(); form.OpenFile(fileName); form.Show(); // Bring form to top form.Activate(); return form; } private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (this.openFileDialog.ShowDialog() == DialogResult.OK) { TopLevelForm.CreateTopLevelWindow(this.openFileDialog.FileName); } } void OpenFile(string fileName) { this.fileName = fileName; if (!string.IsNullOrEmpty(fileName)) { using (StreamReader reader = new StreamReader(fileName)) { textBox.Text = reader.ReadToEnd(); } } else this.fileName = "untitled" + formCount.ToString(); t
-
I want my application to behave like MSWord 2003. i.e. Multiple-SDI Application. I am facing problem with the code that first window always close all other windows. Please identify changes.
//TopLevelForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace MultipleSDI { public partial class TopLevelForm : Form { string fileName; static int formCount = 0; public TopLevelForm() { InitializeComponent(); MultiSDIApplication.Application.AddTopLevelForm(this); MultiSDIApplication.Application.AddWindowMenu(this.windowToolStripMenuItem); ++formCount; this.Text += ": " + formCount.ToString(); } public static TopLevelForm CreateTopLevelWindow(string fileName) { // Detect whether file is already open if (!string.IsNullOrEmpty(fileName)) { foreach (TopLevelForm openForm in Application.OpenForms) { if (string.Compare(openForm.FileName, fileName, true) == 0) { // Bring form to top openForm.Activate(); return openForm; } } } // Create new top-level form and open file TopLevelForm form = new TopLevelForm(); form.OpenFile(fileName); form.Show(); // Bring form to top form.Activate(); return form; } private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (this.openFileDialog.ShowDialog() == DialogResult.OK) { TopLevelForm.CreateTopLevelWindow(this.openFileDialog.FileName); } } void OpenFile(string fileName) { this.fileName = fileName; if (!string.IsNullOrEmpty(fileName)) { using (StreamReader reader = new StreamReader(fileName)) { textBox.Text = reader.ReadToEnd(); } } else this.fileName = "untitled" + formCount.ToString(); t
First of all, putting large amounts of code in a post is not a good idea, since few people will take the time to check it out. And if you don't explain what's going on, it is hard to understand at first glance. But here is an idea: maybe your message loop is running on your first window, so when it is closed the message loop returns and the app is closed. I would create an invisible window, run the message loop on it, and when the last window is closed, kill that window. You can also use that hidden window to get a list of all windows (for the Window menu, for example). I hope this helps.
Luis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!