none
Global bool variable - newbie to C#

    Question

  • I have worked with SQL for several years, so I am familiar with programming logic. My company has tasked me with creating an interface for one of our databases, and I have had a little over a week to become familiar with C#.

    The problem seems like it should be extremely easy to solve, but so far I have not been able to figure it out, nor have I found any web sites that have offered much in the way of answers.

    I have an MDI parent form that opens a navigation child form. To ensure that the navigation child form (and all other forms I'll add to this project) are only opened once (if already open bring to front, if not open then open new) I am attempting to use a global bool variable.

    I cannot seem to reference the global variable from the navigation form. The global variable references a public static class, and the variable itself is public having been declared on the MDI parent form (this of course is still open while the child form is open).

    Copy of live code:
    frmBLD_Main - MDI parent

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmBLD_Main : Form
        {
            public static csWindowControls setScreenStatus;
            frmNavigation newMDIChild = new frmNavigation();
    
            public frmBLD_Main()
            {
                InitializeComponent();
            }
    
            private void frmBLD_Main_Load(object sender, EventArgs e)
            {
                // Set the Parent Form of the Child window.
                newMDIChild.MdiParent = this;
                // Display the new form.
                newMDIChild.Show();
    
                setScreenStatus.navScreenStatus = true;
            }
    
            private void openNavigationScreenToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (setScreenStatus.navScreenStatus == true)
                {
                    newMDIChild.TopMost = true;
                    newMDIChild.Show();
                    newMDIChild.BringToFront();
                }
                else if (setScreenStatus.navScreenStatus == false)
                {
                    
                    // Set the Parent Form of the Child window.
                    newMDIChild.MdiParent = this;
                    // Display the new form.
                    newMDIChild.Show();
    
                    setScreenStatus.navScreenStatus = true;
                }
                
            }
        }
    }

    //frmNavigation
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmNavigation : Form
        {
            public frmNavigation()
            {
                InitializeComponent();
            }
    
            private void frmNavigation_Load(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
                webNavUserNotes.Navigate("C:\\Users\\jarid\\Desktop\\Visual Studio Projects\\C# Programs\\BonfireLoanDatabase User Interface\\BonfireLoanDatabase User Interface\\HTML\\UserNotes.html");
            }
    
            private void frmNavigation_Resize(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
            }
    
            private void frmNavigation_FormClosing(object sender, FormClosingEventArgs e)
            {
                setScreenStatus.navScreenStatus = false; //This is the line that causes the error
            }
        }
    }

    //csWindowControls - public static class
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public static class csWindowControls
        {
            // ============================================
            // Class Variables
            // ============================================
            bool isNavScreenOpen;
    
            // ============================================
            // Default Constructor
            // ============================================
            public csWindowControls()
            {   
            }
    
            // ============================================
            // Method
            // ============================================
    
    
            // ============================================
            // Read and write properties
            // ============================================
            public bool navScreenStatus
            {
                get { return isNavScreenOpen; }
                set { isNavScreenOpen = value; }
            }
    
            // ============================================
            // Write only properties
            // ============================================
        }
    }

    Friday, June 22, 2012 6:23 PM

Answers

  • I'm going to leave the same response here that I left on the other forum you posted this to:

    Ok, let me see if I understand your basic problem.

    1. You have an MDI parent and an MDI child
    2. You only ever want one instance of the child open at a time
    3. You want the child form to communicate when it's closed back to the parent so it can keep track of its state

    Well, the good news is you don't need a "global" bool, nor do you need a static bool. In fact, you don't need to worry about communicating between forms at all, just to do this. All can be handled from your MDI Parent form. Here's a very simple example. In this case, Form1 is the MdiParent, and Form2 and Form3 are different Mdi Children. There are two buttons on a menu strip that will either launch Form1/2, or bring it to the front if it's already open:

    private Form2 _form2;
    private Form3 _form3;
    
    public Form1()
    {
        InitializeComponent();
    }
    
    private void LaunchForm2Click(object sender, EventArgs e)
    {
        if (_form2 == null || _form2.IsDisposed)
        {
            _form2 = new Form2 { MdiParent = this };
        }
    
        if(!_form2.Visible)
        {
            _form2.Show();
        }
        else
        {
            _form2.BringToFront();
        }
    }
    
    private void LaunchForm3Click(object sender, EventArgs e)
    {
        if (_form3 == null || _form3.IsDisposed)
        {
            _form3 = new Form3 { MdiParent = this };
        }
    
        if (!_form3.Visible)
        {
            _form3.Show(); 
        }
        else
        {
            _form3.BringToFront();
        }
    }


    Let me know if I'm off track.

    Side note: you'd run into a bigger issue the way you were doing it. When you Close a form, it is also "Disposed", which means that its resources are cleaned up in preparation for being completely abandoned and garbage collected. So, if you then tried to use the same form again, you're going to run into issues because of disposed resources.

    So, in my solution, I'm creating a new copy of the form if it has already been closed once. If this is unacceptable, you can cancel the closing event in the child form and .Hide() it, and it should still work with my solution.

    Edit: while I'm at it, I'll leave a link to my tutorial for How to Get Values from One Form to Another.
    Tuesday, June 26, 2012 7:42 PM
  • LADIES AND GENTLEMEN, WE HAVE A WINNER!!!

    Curtis, thank you for helping out with this. I took a moment to read your blog post regarding passing values from one form to another. I'll be honest, I don't fully follow all of the details of it yet. I do look forward to attempting to use your techniques during this project and going forward.

    And to officially show the answer to my original question...

    I began a new project to avoid the need to pick out what shouldn't be in the code; it was easier to redo everything from the ground up. Ironically I still have 3 blocks of code to display (and I figured out how to ensure the windows start in the fully maximized form I want them to...my last project had several glitches in that section, but reading some of the posts during this research gave me the idea...sorry, ADHD break).

    The project has 3 windows:

    frmBLD_Main - MDI parent window

    frmNav & frmAddDealer - MDI child windows

    frmBLD_Main has a menu bar with the main heading Program Navigation, and sub items Main Navigation and Add New Dealer

    //frmBLD_Main
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    // =====================================================================
    // Jarid Lawson
    // 6/26/2012
    // Bonfire Loan Database User Interface
    // 
    // This system is to be used to add new dealers, deals, loans, etc. to
    // the BonfireLoanDatabase. Using MDI technique to improve the user
    // experience.
    // =====================================================================
    
    namespace BLD_User_Interface
    {
        public partial class frmBLD_Main : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
            private frmNav _frmNav;
            private frmAddDealer _frmAddDealer;
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmBLD_Main()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmBLD_Main_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
                _frmNav = new frmNav { MdiParent = this };
                _frmNav.Show();
            }
    
            // =====================================================================
            // Menu Strip - Program Navigation > Main Navigation
            // =====================================================================
            private void mainNavigationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (_frmNav == null || _frmNav.IsDisposed)
                {
                    _frmNav = new frmNav { MdiParent = this };
                }
    
                if (!_frmNav.Visible)
                {
                    _frmNav.Show();
                }
                else
                {
                    _frmNav.BringToFront();
                }
            }
    
            // =====================================================================
            // Menu Strip - Program Navigation > Add New Dealer
            // =====================================================================
            private void addNewDealerToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (_frmAddDealer == null || _frmAddDealer.IsDisposed)
                {
                    _frmAddDealer = new frmAddDealer { MdiParent = this };
                }
    
                if (!_frmAddDealer.Visible)
                {
                    _frmAddDealer.Show();
                }
                else
                {
                    _frmAddDealer.BringToFront();
                }
            }
        }
    }
    
    //frmNav
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BLD_User_Interface
    {
        public partial class frmNav : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmNav()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmNav_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
    }
    
    //frmAddDealer
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BLD_User_Interface
    {
        public partial class frmAddDealer : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmAddDealer()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmAddDealer_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
    }
    



    “Any fool can know. The point is to understand.” - Albert Einstein " DOH!" - Homer Simpson

    • Marked as answer by Jarid.Lawson Tuesday, June 26, 2012 10:15 PM
    Tuesday, June 26, 2012 10:15 PM

All replies

  • You need to change your code as follows:

    private void frmNavigation_FormClosing(object sender, FormClosingEventArgs e)
            {
                frmBLD_Main.setScreenStatus.navScreenStatus = false; //This is the line that causes the error
            }
    The keyword "static" does not mean "global"  It means "shared across all instances."  You declared the static variable inside frmBLD_Main, so you must reference it as a member of frmBLD_Main.
    • Proposed as answer by Moby Disk Friday, June 22, 2012 8:00 PM
    • Unproposed as answer by Jarid.Lawson Tuesday, June 26, 2012 3:50 PM
    Friday, June 22, 2012 8:00 PM
  • I made the change recommended, and still am receiving errors. I have tried a couple of variations, and am listing as many of the attempts with the associated errors as I can.

    Attempt 1: The suggested change

    //frmBLD_Main
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmBLD_Main : Form
        {
            public static csWindowControls setScreenStatus;
            frmNavigation newMDIChild = new frmNavigation();
    
            public frmBLD_Main()
            {
                InitializeComponent();
            }
    
            private void frmBLD_Main_Load(object sender, EventArgs e)
            {
                // Set the Parent Form of the Child window.
                newMDIChild.MdiParent = this;
                // Display the new form.
                newMDIChild.Show();
    
                setScreenStatus.navScreenStatus = true;
            }
    
            private void openNavigationScreenToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (setScreenStatus.navScreenStatus == true)
                {
                    newMDIChild.TopMost = true;
                    newMDIChild.Show();
                    newMDIChild.BringToFront();
                }
                else if (setScreenStatus.navScreenStatus == false)
                {
                    
                    // Set the Parent Form of the Child window.
                    newMDIChild.MdiParent = this;
                    // Display the new form.
                    newMDIChild.Show();
    
                    setScreenStatus.navScreenStatus = true;
                }
                
            }
        }
    }
    
    //frmNavigation
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmNavigation : Form
        {
            public frmNavigation()
            {
                InitializeComponent();
            }
    
            private void frmNavigation_Load(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
                webNavUserNotes.Navigate("C:\\Users\\jarid\\Desktop\\Visual Studio Projects\\C# Programs\\BonfireLoanDatabase User Interface\\BonfireLoanDatabase User Interface\\HTML\\UserNotes.html");
            }
    
            private void frmNavigation_Resize(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
            }
    
            private void frmNavigation_FormClosing(object sender, FormClosingEventArgs e)
            {
                frmBLD_Main.setScreenStatus.navScreenStatus = false;
            }
        }
    }
    


    //csWindowsControls
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public static class csWindowControls
        {
            // ============================================
            // Class Variables
            // ============================================
            bool isNavScreenOpen; //Error	1	'BonfireLoanDatabase_User_Interface.csWindowControls.isNavScreenOpen': cannot declare instance members in a static class	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\BonfireLoanDatabase User Interface\BonfireLoanDatabase User Interface\csWindowControls.cs	13	14	BonfireLoanDatabase User Interface
    
    
            // ============================================
            // Default Constructor
            // ============================================
            public csWindowControls() //Error	2	Static classes cannot have instance constructors	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\BonfireLoanDatabase User Interface\BonfireLoanDatabase User Interface\csWindowControls.cs	18	16	BonfireLoanDatabase User Interface
    
            {   
            }
    
            // ============================================
            // Method
            // ============================================
    
    
            // ============================================
            // Read and write properties
            // ============================================
            public bool navScreenStatus //Error	3	'BonfireLoanDatabase_User_Interface.csWindowControls.navScreenStatus': cannot declare instance members in a static class	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\BonfireLoanDatabase User Interface\BonfireLoanDatabase User Interface\csWindowControls.cs	30	21	BonfireLoanDatabase User Interface
    
            {
                get { return isNavScreenOpen; }
                set { isNavScreenOpen = value; }
            }
    
            // ============================================
            // Write only properties
            // ============================================
        }
    }
    

    My understanding of this based on a guess, but I believe I'm correct. Since the class csWindowControls is a static class I think and I stress I only THINK this means it cannot have variables (public or private).

    Continued in the next post...


    “Any fool can know. The point is to understand.” - Albert Einstein "DOH!" - Homer Simpson

    Tuesday, June 26, 2012 3:15 PM
  • To address the last error, I reset the class from a static class to a standard public class.

    Attempt 2: Remove from static class

    //frmBLD_Main
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmBLD_Main : Form
        {
            public csWindowControls setScreenStatus = new csWindowControls();
            frmNavigation newMDIChild = new frmNavigation();
    
            public frmBLD_Main()
            {
                InitializeComponent();
            }
    
            private void frmBLD_Main_Load(object sender, EventArgs e)
            {
                // Set the Parent Form of the Child window.
                newMDIChild.MdiParent = this;
                // Display the new form.
                newMDIChild.Show();
    
                setScreenStatus.navScreenStatus = true;
            }
    
            private void openNavigationScreenToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (setScreenStatus.navScreenStatus == true)
                {
                    newMDIChild.TopMost = true;
                    newMDIChild.Show();
                    newMDIChild.BringToFront();
                }
                else if (setScreenStatus.navScreenStatus == false)
                {
                    
                    // Set the Parent Form of the Child window.
                    newMDIChild.MdiParent = this;
                    // Display the new form.
                    newMDIChild.Show();
    
                    setScreenStatus.navScreenStatus = true;
                }
                
            }
        }
    }
    

    //frmNavigation
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public partial class frmNavigation : Form
        {
            public frmNavigation()
            {
                InitializeComponent();
            }
    
            private void frmNavigation_Load(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
                webNavUserNotes.Navigate("C:\\Users\\jarid\\Desktop\\Visual Studio Projects\\C# Programs\\BonfireLoanDatabase User Interface\\BonfireLoanDatabase User Interface\\HTML\\UserNotes.html");
            }
    
            private void frmNavigation_Resize(object sender, EventArgs e)
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
            }
    
            private void frmNavigation_FormClosing(object sender, FormClosingEventArgs e)
            {
                frmBLD_Main.setScreenStatus.navScreenStatus = false; //Error	1	An object reference is required for the non-static field, method, or property 'BonfireLoanDatabase_User_Interface.frmBLD_Main.setScreenStatus'	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\BonfireLoanDatabase User Interface\BonfireLoanDatabase User Interface\frmNavigation.cs	33	13	BonfireLoanDatabase User Interface
    
            }
        }
    }
    

    //csWindowControls
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BonfireLoanDatabase_User_Interface
    {
        public class csWindowControls
        {
            // ============================================
            // Class Variables
            // ============================================
            bool isNavScreenOpen;
    
            // ============================================
            // Default Constructor
            // ============================================
            public csWindowControls()
            {   
            }
    
            // ============================================
            // Method
            // ============================================
    
    
            // ============================================
            // Read and write properties
            // ============================================
            public bool navScreenStatus
            {
                get { return isNavScreenOpen; }
                set { isNavScreenOpen = value; }
            }
    
            // ============================================
            // Write only properties
            // ============================================
        }
    }
    

    I am not sure why this would still come up. I have referenced the instancing object, but it still fails. I'm still doing research, and will keep updating as I make attempts, but while I look I figured I'd raise the flag.


    “Any fool can know. The point is to understand.” - Albert Einstein "DOH!" - Homer Simpson

    Tuesday, June 26, 2012 3:29 PM
  • Another question has come into mind with some of the research I have done: am I approaching my task with the correct solution?

    To recap; what I am attempting to do is to ensure that my MDI child forms are opened only once. I need this tight control because I am not using them like an Excel worksheet. Instead I'm using them as different screens in a program: these are Navigation, Add New Record, Search Records, etc. screens. I don't care at the moment if one screen closes when another opens or not, but I do want to check if there is already a screen open before I try to re-open the same screen. If it is already open I can simply set that child form = HasFocus, else open child form.

    I have seen references to iterate through the child forms, but I am not sure I fully understand that concept yet. Before I dig in further I wanted to check if this is even a valid means of accomplishing my task, or if I am better off sticking with my current approach (that still doesn't work) of setting a bool variable in a public class.

    And as I type this another solution idea comes to mind...instead of creating the class outside of the MDI parent, maybe I'll try to create that class within the MDI parent. Then when I reference the MDI parent I will still have access to the sub-class instance of csWindowControls.

    I'll try this and update with the results, but in the mean time if anyone has any ideas of how to do what I'm talking about I'd greatly appreciate any direction you can offer.


    “Any fool can know. The point is to understand.” - Albert Einstein " DOH!" - Homer Simpson

    Tuesday, June 26, 2012 5:05 PM
  • I was hoping that removing the .cs file separation from the main form (frmBLD_Main.cs) and the csWindowControls class (csWindowControls.cs) would be a solution. This attempt failed.

    For this attempt I wanted to eliminate the potential form name confusion, and not risk needing to replace all instances of one name with another in my active project. As this is a basic technique I am going with the thought that if I can set this value in a random program I can do it in any other program in the same general manor, so I created a new project with 3 forms.

    For those playing along at home, here are the details you'll need

    Form1 - MDI parent

    Form2 & Form 3 - MDI children

    I called Form2 and Form3 from a menu strip added to Form1. The top level value is Add..., with 2 sub-values of New Form 2 and New Form 3.

    Both Form2 and Form3 only had a label on each to show which form was being displayed. From there, this was the code I added (note I tried several different versions of accessing the class on Form1 in Form2 and Form3, but I only had room enough to leave the 2 most obvious attempts to show the resulting errors):

    //Form1
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace C_Sharp_Lessons___MDI_ChildForm_Open_Research
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            csWindowControls childFormStatus = new csWindowControls();
    
            private void newForm2ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (childFormStatus.form2ScreenStatus == false)
                {
                    // Create a new instance of the child form, Form2.
                    Form2 childForm2 = new Form2();
                    // Make it a child of this MDI form before showing it.
                    childForm2.MdiParent = this;
                    childForm2.Show();
    
                    childFormStatus.form2ScreenStatus = true;
                }
                else
                {
                    MessageBox.Show("Form2 already open");
                }
            }
    
            private void newForm3ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (childFormStatus.form3ScreenStatus == false)
                {
                    // Create a new instance of the child form, Form3.
                    Form3 childForm3 = new Form3();
                    // Make it a child of this MDI form before showing it.
                    childForm3.MdiParent = this;
                    childForm3.Show();
    
                    childFormStatus.form3ScreenStatus = true;
                }
                else
                {
                    MessageBox.Show("Form 3 already open");
                }
            }
    
            public class csWindowControls : Form1
            {
                // ============================================
                // Class Variables
                // ============================================
                bool isFrm2ScreenOpen;
                bool isFrm3ScreenOpen;
    
                // ============================================
                // Default Constructor
                // ============================================
                public csWindowControls()
                {
                }
    
                // ============================================
                // Method
                // ============================================
    
    
                // ============================================
                // Read and write properties
                // ============================================
                public bool form2ScreenStatus
                {
                    get { return isFrm2ScreenOpen; }
                    set { isFrm2ScreenOpen = value; }
                }
    
                public bool form3ScreenStatus
                {
                    get { return isFrm3ScreenOpen; }
                    set { isFrm3ScreenOpen = value; }
                }
    
                // ============================================
                // Write only properties
                // ============================================
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                childFormStatus.form2ScreenStatus = false;
                childFormStatus.form3ScreenStatus = false;
            }
        }
    }
    

    //Form2
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace C_Sharp_Lessons___MDI_ChildForm_Open_Research
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                Form1.csWindowControls.form2ScreenStatus = false; //Error	3	An object reference is required for the non-static field, method, or property 'C_Sharp_Lessons___MDI_ChildForm_Open_Research.Form1.csWindowControls.form2ScreenStatus.get'	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\C Sharp Lessons\C_Sharp Lessons - MDI ChildForm Open Research\C_Sharp Lessons - MDI ChildForm Open Research\Form2.cs	22	13	C_Sharp Lessons - MDI ChildForm Open Research
    
            }
        }
    }
    
    //Form3
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace C_Sharp_Lessons___MDI_ChildForm_Open_Research
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            private void Form3_FormClosed(object sender, FormClosedEventArgs e)
            {
                Form1.childFormStatus = false; //Error	1	'C_Sharp_Lessons___MDI_ChildForm_Open_Research.Form1.childFormStatus' is inaccessible due to its protection level	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\C Sharp Lessons\C_Sharp Lessons - MDI ChildForm Open Research\C_Sharp Lessons - MDI ChildForm Open Research\Form3.cs	22	19	C_Sharp Lessons - MDI ChildForm Open Research
    
    //Error	2	An object reference is required for the non-static field, method, or property 'C_Sharp_Lessons___MDI_ChildForm_Open_Research.Form1.childFormStatus'	C:\Users\jarid\Desktop\Visual Studio Projects\C# Programs\C Sharp Lessons\C_Sharp Lessons - MDI ChildForm Open Research\C_Sharp Lessons - MDI ChildForm Open Research\Form3.cs	22	13	C_Sharp Lessons - MDI ChildForm Open Research
    
            }
        }
    }
    
    I'm running out of ideas, and am becoming troubled with not finding an answer to a task that looks like it should be very basic. Passing a value from one screen to the next is something that is a fairly common task from my end user level experience. Every Wizard I can think of includes a reference to the last screen's settings and values. What am I missing?


    “Any fool can know. The point is to understand.” - Albert Einstein " DOH!" - Homer Simpson

    Tuesday, June 26, 2012 5:59 PM
  • I'm going to leave the same response here that I left on the other forum you posted this to:

    Ok, let me see if I understand your basic problem.

    1. You have an MDI parent and an MDI child
    2. You only ever want one instance of the child open at a time
    3. You want the child form to communicate when it's closed back to the parent so it can keep track of its state

    Well, the good news is you don't need a "global" bool, nor do you need a static bool. In fact, you don't need to worry about communicating between forms at all, just to do this. All can be handled from your MDI Parent form. Here's a very simple example. In this case, Form1 is the MdiParent, and Form2 and Form3 are different Mdi Children. There are two buttons on a menu strip that will either launch Form1/2, or bring it to the front if it's already open:

    private Form2 _form2;
    private Form3 _form3;
    
    public Form1()
    {
        InitializeComponent();
    }
    
    private void LaunchForm2Click(object sender, EventArgs e)
    {
        if (_form2 == null || _form2.IsDisposed)
        {
            _form2 = new Form2 { MdiParent = this };
        }
    
        if(!_form2.Visible)
        {
            _form2.Show();
        }
        else
        {
            _form2.BringToFront();
        }
    }
    
    private void LaunchForm3Click(object sender, EventArgs e)
    {
        if (_form3 == null || _form3.IsDisposed)
        {
            _form3 = new Form3 { MdiParent = this };
        }
    
        if (!_form3.Visible)
        {
            _form3.Show(); 
        }
        else
        {
            _form3.BringToFront();
        }
    }


    Let me know if I'm off track.

    Side note: you'd run into a bigger issue the way you were doing it. When you Close a form, it is also "Disposed", which means that its resources are cleaned up in preparation for being completely abandoned and garbage collected. So, if you then tried to use the same form again, you're going to run into issues because of disposed resources.

    So, in my solution, I'm creating a new copy of the form if it has already been closed once. If this is unacceptable, you can cancel the closing event in the child form and .Hide() it, and it should still work with my solution.

    Edit: while I'm at it, I'll leave a link to my tutorial for How to Get Values from One Form to Another.
    Tuesday, June 26, 2012 7:42 PM
  • Hi, I would strongly suggest you NOT to use any static classes, methods and members of any class.

    This way you will avoid inconveniances.

    Next, use events to go from form to form.

    What is more next, since you didnt provide us any decent explanation what you would like to do, we cannot offer you any decent help.

    Please explain (in a short) what is the main purpose of this code of yours.


    Mitja


    • Edited by Mitja Bonca Tuesday, June 26, 2012 8:39 PM
    • Proposed as answer by JohnGrove Tuesday, June 26, 2012 9:16 PM
    Tuesday, June 26, 2012 8:37 PM
  • LADIES AND GENTLEMEN, WE HAVE A WINNER!!!

    Curtis, thank you for helping out with this. I took a moment to read your blog post regarding passing values from one form to another. I'll be honest, I don't fully follow all of the details of it yet. I do look forward to attempting to use your techniques during this project and going forward.

    And to officially show the answer to my original question...

    I began a new project to avoid the need to pick out what shouldn't be in the code; it was easier to redo everything from the ground up. Ironically I still have 3 blocks of code to display (and I figured out how to ensure the windows start in the fully maximized form I want them to...my last project had several glitches in that section, but reading some of the posts during this research gave me the idea...sorry, ADHD break).

    The project has 3 windows:

    frmBLD_Main - MDI parent window

    frmNav & frmAddDealer - MDI child windows

    frmBLD_Main has a menu bar with the main heading Program Navigation, and sub items Main Navigation and Add New Dealer

    //frmBLD_Main
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    // =====================================================================
    // Jarid Lawson
    // 6/26/2012
    // Bonfire Loan Database User Interface
    // 
    // This system is to be used to add new dealers, deals, loans, etc. to
    // the BonfireLoanDatabase. Using MDI technique to improve the user
    // experience.
    // =====================================================================
    
    namespace BLD_User_Interface
    {
        public partial class frmBLD_Main : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
            private frmNav _frmNav;
            private frmAddDealer _frmAddDealer;
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmBLD_Main()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmBLD_Main_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
                _frmNav = new frmNav { MdiParent = this };
                _frmNav.Show();
            }
    
            // =====================================================================
            // Menu Strip - Program Navigation > Main Navigation
            // =====================================================================
            private void mainNavigationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (_frmNav == null || _frmNav.IsDisposed)
                {
                    _frmNav = new frmNav { MdiParent = this };
                }
    
                if (!_frmNav.Visible)
                {
                    _frmNav.Show();
                }
                else
                {
                    _frmNav.BringToFront();
                }
            }
    
            // =====================================================================
            // Menu Strip - Program Navigation > Add New Dealer
            // =====================================================================
            private void addNewDealerToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (_frmAddDealer == null || _frmAddDealer.IsDisposed)
                {
                    _frmAddDealer = new frmAddDealer { MdiParent = this };
                }
    
                if (!_frmAddDealer.Visible)
                {
                    _frmAddDealer.Show();
                }
                else
                {
                    _frmAddDealer.BringToFront();
                }
            }
        }
    }
    
    //frmNav
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BLD_User_Interface
    {
        public partial class frmNav : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmNav()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmNav_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
    }
    
    //frmAddDealer
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BLD_User_Interface
    {
        public partial class frmAddDealer : Form
        {
            // =====================================================================
            // Class level variables
            // =====================================================================
    
            // =====================================================================
            // Constructor
            // =====================================================================
            public frmAddDealer()
            {
                InitializeComponent();
            }
    
            // =====================================================================
            // On Load
            // =====================================================================
            private void frmAddDealer_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
    }
    



    “Any fool can know. The point is to understand.” - Albert Einstein " DOH!" - Homer Simpson

    • Marked as answer by Jarid.Lawson Tuesday, June 26, 2012 10:15 PM
    Tuesday, June 26, 2012 10:15 PM