Click here to Skip to main content
16,013,322 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question!!! Pin
Not Active8-Dec-06 14:38
mentorNot Active8-Dec-06 14:38 
AnswerRe: Question!!! Pin
Pete O'Hanlon8-Dec-06 9:54
mvePete O'Hanlon8-Dec-06 9:54 
AnswerRe: Question!!! Pin
dead_link8-Dec-06 9:58
dead_link8-Dec-06 9:58 
GeneralRe: Question!!! Pin
Dan Neely8-Dec-06 10:46
Dan Neely8-Dec-06 10:46 
GeneralRe: Question!!! Pin
likefood8-Dec-06 14:13
likefood8-Dec-06 14:13 
GeneralRe: Question!!! Pin
Dan Neely8-Dec-06 14:17
Dan Neely8-Dec-06 14:17 
GeneralRe: Question!!! Pin
dead_link9-Dec-06 0:22
dead_link9-Dec-06 0:22 
QuestionMultiple-SDI Application Pin
h@s@n8-Dec-06 8:17
h@s@n8-Dec-06 8:17 
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.


<br />
//TopLevelForm.cs<br />
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.IO;<br />
<br />
<br />
namespace MultipleSDI<br />
{<br />
    public partial class TopLevelForm : Form<br />
    {<br />
<br />
        string fileName;<br />
        static int formCount = 0;<br />
<br />
        public TopLevelForm()<br />
        {<br />
            InitializeComponent();<br />
<br />
            MultiSDIApplication.Application.AddTopLevelForm(this);<br />
            MultiSDIApplication.Application.AddWindowMenu(this.windowToolStripMenuItem);<br />
<br />
                        <br />
            ++formCount;<br />
            this.Text += ": " + formCount.ToString();<br />
        }<br />
       <br />
 <br />
        public static TopLevelForm CreateTopLevelWindow(string fileName) <br />
        {<br />
            <br />
        // Detect whether file is already open<br />
            if (!string.IsNullOrEmpty(fileName))<br />
            {<br />
                foreach (TopLevelForm openForm in Application.OpenForms)<br />
                {<br />
                    if (string.Compare(openForm.FileName, fileName, true) == 0)<br />
                    {<br />
                        // Bring form to top<br />
                        openForm.Activate();<br />
                        return openForm;<br />
                    }<br />
                }<br />
            }<br />
            <br />
            // Create new top-level form and open file<br />
            TopLevelForm form = new TopLevelForm();<br />
            form.OpenFile(fileName);<br />
            form.Show();<br />
<br />
                // Bring form to top<br />
                form.Activate();<br />
                return form;<br />
           <br />
        }<br />
<br />
        private void openToolStripMenuItem_Click(object sender, EventArgs e)<br />
        {<br />
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)<br />
            {<br />
                TopLevelForm.CreateTopLevelWindow(this.openFileDialog.FileName);<br />
            }<br />
<br />
        }<br />
<br />
        void OpenFile(string fileName)<br />
        {<br />
            this.fileName = fileName;<br />
            if (!string.IsNullOrEmpty(fileName))<br />
            {<br />
                using (StreamReader reader = new StreamReader(fileName))<br />
                {<br />
                    textBox.Text = reader.ReadToEnd();<br />
                }<br />
            }<br />
            else this.fileName = "untitled" + formCount.ToString();<br />
            this.Text = this.Text + " (" + this.fileName + ")";<br />
        }<br />
<br />
        string FileName<br />
        {<br />
            get { return this.fileName; }<br />
        }<br />
<br />
        private void newToolStripMenuItem_Click(object sender, EventArgs e)<br />
        {<br />
            TopLevelForm.CreateTopLevelWindow(null);<br />
        }<br />
<br />
<br />
<br />
    }<br />
}<br />
<br />
//MultiSDIApplication<br />
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using Microsoft.VisualBasic.ApplicationServices;<br />
using System.Collections.ObjectModel;<br />
<br />
<br />
<br />
namespace MultipleSDI<br />
{<br />
    class MultiSDIApplication : WindowsFormsApplicationBase<br />
    {<br />
<br />
        static MultiSDIApplication application;<br />
        internal static MultiSDIApplication Application<br />
        {<br />
            get<br />
            {<br />
                if (application == null)<br />
                {<br />
                    application = new MultiSDIApplication();<br />
                }<br />
                return application;<br />
            }<br />
        }<br />
<br />
        public MultiSDIApplication()<br />
        {<br />
<br />
<br />
            // This ensures the underlying single-SDI framework is employed, <br />
            // and OnStartupNextInstance is fired<br />
<br />
            this.IsSingleInstance = true;<br />
<br />
<br />
            // Needed for multiple SDI because no form is the main form<br />
            this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;// AfterMainFormCloses;<br />
<br />
<br />
        }<br />
<br />
        protected override void OnCreateMainForm()<br />
        {<br />
            this.MainForm = this.CreateTopLevelWindow(this.CommandLineArgs);<br />
        }<br />
<br />
        TopLevelForm CreateTopLevelWindow(ReadOnlyCollection<string> args)<br />
        {<br />
<br />
            // Get file name, if provided<br />
            string fileName = (args.Count > 0 ? args[0] : null);<br />
<br />
            // Create a new top-level form<br />
            return TopLevelForm.CreateTopLevelWindow(fileName);<br />
        }<br />
<br />
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)<br />
        {<br />
            this.CreateTopLevelWindow(e.CommandLine);<br />
        }<br />
<br />
        public void AddTopLevelForm(Form form)<br />
        {<br />
<br />
            // Add form to collection of forms and<br />
            // watch for it to activate and close<br />
            form.Activated += Form_Activated;<br />
            form.FormClosed += Form_FormClosed;<br />
<br />
            // Set initial top-level form to activate<br />
            if (this.OpenForms.Count == 1) this.MainForm = form;<br />
        }<br />
<br />
        void Form_Activated(object sender, EventArgs e) <br />
        {<br />
            // Set the currently active form<br />
            this.MainForm = (Form)sender;<br />
        }<br />
 <br />
        void Form_FormClosed(object sender, FormClosedEventArgs e) <br />
        {<br />
            // Set a new "main" if necessary<br />
            if( ((Form)sender == this.MainForm) && (this.OpenForms.Count > 0) ) <br />
            {<br />
                MessageBox.Show("I reached here" + this.OpenForms.Count.ToString());<br />
                this.MainForm = (Form)this.OpenForms[0];<br />
            }<br />
 <br />
            MainForm.Activated -= Form_Activated;<br />
            MainForm.FormClosed -= Form_FormClosed;<br />
        }<br />
<br />
        public void AddWindowMenu(ToolStripMenuItem windowMenu) <br />
        {<br />
                // Handle tool strip menu item’s drop-down opening event<br />
                windowMenu.DropDownOpening += windowMenu_DropDownOpening;<br />
        }<br />
<br />
        <br />
        void windowMenu_DropDownOpening(object sender, EventArgs e) <br />
        {<br />
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;<br />
 <br />
            // Clear current menu<br />
            if( menu.DropDownItems.Count > 0 ) <br />
            {<br />
                menu.DropDown.Dispose();<br />
            }<br />
            <br />
            menu.DropDown = new ToolStripDropDown();<br />
 <br />
            // Populate menu with one item for each open top-level form<br />
            foreach( Form form in this.OpenForms ) <br />
            {<br />
                ToolStripMenuItem item = new ToolStripMenuItem();<br />
                item.Text = form.Text;<br />
                item.Tag = form;<br />
                menu.DropDownItems.Add(item);<br />
                item.Click += WindowMenuItem_Click;<br />
 <br />
             // Check menu item that represents currently active window<br />
            if( form == this.MainForm ) item.Checked = true;<br />
            }<br />
        }<br />
<br />
        void WindowMenuItem_Click(object sender, EventArgs e)<br />
        {<br />
            // Activate top-level form based on selection<br />
            ((Form)((ToolStripMenuItem)sender).Tag).Activate();<br />
        }<br />
<br />
<br />
<br />
    }<br />
}<br />
<br />
<br />
<br />

AnswerRe: Multiple-SDI Application Pin
Luis Alonso Ramos9-Dec-06 19:23
Luis Alonso Ramos9-Dec-06 19:23 
QuestionUsing reflection to get the instance name of a variable Pin
EddieRich8-Dec-06 8:13
EddieRich8-Dec-06 8:13 
AnswerRe: Using reflection to get the instance name of a variable Pin
Andrew Rissing8-Dec-06 8:35
Andrew Rissing8-Dec-06 8:35 
GeneralRe: Using reflection to get the instance name of a variable Pin
EddieRich8-Dec-06 8:40
EddieRich8-Dec-06 8:40 
GeneralRe: Using reflection to get the instance name of a variable Pin
Not Active8-Dec-06 8:57
mentorNot Active8-Dec-06 8:57 
GeneralRe: Using reflection to get the instance name of a variable Pin
EddieRich8-Dec-06 9:02
EddieRich8-Dec-06 9:02 
GeneralRe: Using reflection to get the instance name of a variable Pin
Not Active8-Dec-06 9:25
mentorNot Active8-Dec-06 9:25 
GeneralRe: Using reflection to get the instance name of a variable Pin
Pete O'Hanlon8-Dec-06 9:27
mvePete O'Hanlon8-Dec-06 9:27 
AnswerRe: Using reflection to get the instance name of a variable Pin
karam chandrabose8-Dec-06 10:11
karam chandrabose8-Dec-06 10:11 
QuestionAccess DB wont Update Pin
Vodstok8-Dec-06 8:11
Vodstok8-Dec-06 8:11 
AnswerRe: Access DB wont Update Pin
Vodstok8-Dec-06 8:40
Vodstok8-Dec-06 8:40 
QuestionNHibernate Pin
peshawarcoder8-Dec-06 7:52
peshawarcoder8-Dec-06 7:52 
AnswerRe: NHibernate Pin
Pete O'Hanlon8-Dec-06 9:28
mvePete O'Hanlon8-Dec-06 9:28 
GeneralRe: NHibernate Pin
peshawarcoder11-Dec-06 2:12
peshawarcoder11-Dec-06 2:12 
QuestionDatabase Question [modified] Pin
mfcuser8-Dec-06 7:44
mfcuser8-Dec-06 7:44 
AnswerRe: Database Question Pin
User 17164929-Dec-06 4:55
professionalUser 17164929-Dec-06 4:55 
QuestionRestaurant POS Database Question (long) Pin
jeweladdict8-Dec-06 7:29
jeweladdict8-Dec-06 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.