( Index )
Month

 

Brief Information about the February 2015 CSIG Meeting

THREE FORMS.cs - A Simple Demonstration App for Multiple Forms ......    by B. Arnold

LATE BREAKING NEWS:   http://www.techsupportalert.com/content/microsoft-visual-studio-now-free-non-commercial-use.htm

(General Csig Info)    This month's application is a simple C-sharp program that deals with multiple windows. Although programmed in C# it is easily converted to C++ since it uses the DOT NET libraries. See below for further information.

Sample

The program was prompted by an email that I received from a programmer:

"I have always had trouble with starting a C# windows project when multiple windows are needed.  And these would be sort of sibling windows, not parent child.  Sort of a multiple window program like TurboTax, where you might have 2 or more windows open, all  doing "stuff".  The windows are modal-less (?) in the sense that they don't block each other.

"The problem I had was how to create that second form using the IDE in a natural fashion, and even the first form(!) and be able to communicate in s/w back and forth.  Especially communicating from the sibling window back to the main (form1).  There didn't seem to be any material on MSDN, CodeProject, StackOverflow, etc.  Also examples that sort of came close, either didn't compile, or didn't work.  Some would keep creating the window over and over, that is, the examples didn't sense whether or not the window was already open. 

"The big missing piece was how does code in form2.cs as it were, reference the form1 class, which it appears is "anonymous" by the code generator in the startup code generating the main()."
 


Sample     Sample     Sample     Sample

Sample of Multiple Dialog Boxes

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs. (Some are FREE!)

This is a CLI DOT NET program is coded using C-sharp (C#) and is very similar to C++.  It also still uses the same DOT NET libraries.

The program uses a number of DOT NET Library functions including the following:
Load  Windows Forms CLASSES Timers
Application.Run  context  ExitThread  Close
Show OpenForms  C# Start
WindowState  Button_Click  DOT NET  

There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 4.5
C++ 9.0, etc.
.Net 4.5, etc.
CLI
Common Language Infrastructure

Sample Code

/* ******************************************************************************************
 *  Program.cs    B.Arnold    Feb. 17, 2015
 *
 * This is the startup module in a project called "THREE FROMS".  It is a simple 
 * demonstration of multiple windows in a C-sharp program.  Each window acts independently
 * but can communicate with each of the other windows via global memory. Each window 
 * accepts input from its respective actor. The "Captain" window (form1) also has two
 * buttons for activating and granting focus to either of the other two windows.
 * 
 * The windows are all started (Instantiated) here. Contained within is
 * the "C Main()" procedure or method. This module is also responsible for closing all
 * windows whenever the "Captain" window is closed.
 * 
 * The program is compiled with Microsoft Visual Studio Express 2013 and targets
 * DOT NET FRAMEWORK 4.0
   ****************************************************************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Threeforms
    {
    // The class that handles the creation of the windows
    class MyApplicationContext: ApplicationContext
        {
        public static Captain mCaptainForm = new Captain();
        public static FirstOfficer mFirstOfficerForm = new FirstOfficer();
        public static Engineer mEngineerForm = new Engineer();
        
        private MyApplicationContext() 
            {
            mCaptainForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MyFormClosed);

            // Show three forms.
            mEngineerForm.Show();
            mFirstOfficerForm.Show();
            mCaptainForm.Show();
            }

        private void MyFormClosed(object sender, FormClosedEventArgs e)
            {
            ExitThread();			// "That's all folks."
            }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
            {
            Application.EnableVisualStyles();
            // Application.SetCompatibleTextRenderingDefault(false);

            // Create the MyApplicationContext, and set an Exit strategy.
            //
            MyApplicationContext context = new MyApplicationContext();

            // Run the application with the specific context.
            Application.Run(context);
            }
        }
    }

Sample Code from Past Meetings

// FORM1.H      ACGNJ CSIG MEETING December 16, 2014 - WRITTEN BY BRUCE ARNOLD

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace Circles
    {

    public partial class Form1 : Form
        {
        const int FIFTY_CIRCLES = 50;
        const int DIAMETER = 250;
        const int TIMER_INTERVAL = 2000;    // milliseconds
        static ArrayList CircleArray = new ArrayList();

        public Form1()
            {
            InitializeComponent();
            this.DoubleBuffered = true;   // for better, smoother screen updates.
            this.WindowState = FormWindowState.Maximized;
            }
        ~Form1()
            {
            CircleArray.RemoveRange(0, CircleArray.Count);      // Clear memory.
            }


        // Here is where we create a brand new circle. The default constructor of "Circle" 
        // then calls the constructors of its "base" classes. Remember: "It's turtles all
        // the way down!" A loop creates 50 circles at a time.
        private void timer1_Tick(object sender, EventArgs e)
            {
            this.timer1.Interval = TIMER_INTERVAL;
            int windowWidth = this.ClientRectangle.Width;
            int windowHeight = this.ClientRectangle.Height;
            Random autoRand = new Random();
            int xorg, yorg;
            CircleArray.RemoveRange(0, CircleArray.Count);      // Clear memory.
            for (int loop = 0; loop < FIFTY_CIRCLES; ++loop)
                {
                Circle cc = new Circle();      // Create new circle.
                cc.Diameter(windowHeight / 4);
                int bkg = autoRand.Next(9);
                switch (bkg)
                    {
                    case 0: cc.BkGround(Brushes.SandyBrown); break;       // Random
                    case 1: cc.BkGround(Brushes.Red); break;           // color
                    case 2: cc.BkGround(Brushes.Orange); break;           // pick.
                    case 3: cc.BkGround(Brushes.Yellow); break;
                    case 4: cc.BkGround(Brushes.Green); break;
                    case 5: cc.BkGround(Brushes.Blue); break;
                    case 6: cc.BkGround(Brushes.Violet); break;
                    case 7: cc.BkGround(Brushes.LightGray); break;
                    case 8: cc.BkGround(Brushes.White); break;
                    }
                xorg = windowWidth - cc.cx + 200; if (xorg < 0) xorg = 0;     // Calculate limits.
                yorg = windowHeight - cc.cy + 200; if (yorg < 0) yorg = 0;

                xorg = autoRand.Next(xorg) - 100;     // Randomize values
                yorg = autoRand.Next(yorg) - 100;

                cc.Position(xorg, yorg);
                CircleArray.Add(cc);
                }
            Invalidate();
            }

        private void Form1_Paint(object sender, PaintEventArgs e)
            {
            foreach (Circle obj in CircleArray)
                {
                obj.Paint(e);
                }
            }

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page