Author Topic: FormChangeControls for AdvancedHMIcs project (Csharp)  (Read 4396 times)

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
FormChangeControls for AdvancedHMIcs project (Csharp)
« on: April 25, 2020, 01:16:20 AM »
For those who might be using the AdvancedHMIcs project as the main project, in order to have access to the FormChangeControls and MainMenuDriven controls then just download the attached file to add these controls.

You will need to create the same folder structure in the AdvancedHMIcs project, meaning create the FormChangeControls folder and then create the MainMenuDriven sub-folder.

Then add as existing item to corresponding folders each of these 4 files ( ONE-BY-ONE ) :

FormChangeButton.cs, FormChangeImage.cs, MainMenuButton.cs and MainMenu.cs.

Don't add any other files since they will be imported automatically.

In order to start the app with the MainMenu, modify the following line in the Program.cs file:

            Application.Run(new MainForm());
to
            Application.Run(new MainMenu());

You should also consider having this code behind the MainForm, just in case if this form is set as the startup form and a user is to click its "X" button:

Code: [Select]
namespace AdvancedHMICS
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //*******************************************************************************
        //* Stop polling when the form is not visible in order to reduce communications
        //* Copy this section of code to every new form created
        //*******************************************************************************

        private bool NotFirstShow;
        private void Form_VisibleChanged(object sender, System.EventArgs e)
        {
            //* Do not start comms on first show in case it was set to disable in design mode
            if (NotFirstShow)
                AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, this);
            else
                NotFirstShow = true;
        }

        //***************************************************************
        //* If MainMenu is active then hide this form, else
        //* .NET does not close hidden forms, so do it here
        //* to make sure forms are disposed and drivers close.
        //***************************************************************
        private void MainForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            bool mainMenuActive = false;
            foreach (System.Windows.Forms.Form frm in System.Windows.Forms.Application.OpenForms)
            {
                //* Check to see if the MainMenu is a form in the list
                if (frm.Name == "MainMenu")
                {
                    mainMenuActive = true;
                    break;
                }
            }
            if (mainMenuActive)
                this.Hide();
            else
            {
                int index = 0;
                while (index < System.Windows.Forms.Application.OpenForms.Count)
                {
                    if (System.Windows.Forms.Application.OpenForms[index] == this)
                        index += 1; //* Do not force close this form
                    else
                        System.Windows.Forms.Application.OpenForms[index].Close();
                }
            }
        }
    }
}

For this particular code, you would need to open the MainForm in the designer and in its Properties window click the lightning bolt icon and make sure that the VisibleChanged and FormClosing events have the corresponding sub listed next to it.

FYI, the MainMenu can be positioned on the Top / Left / Right / Bottom of the screen. This is determined by setting the property "MenuPosition" and shaping the form itself (Width vs Height). Currently, the form to be opened will be stretched in one direction to fill the screen size.

« Last Edit: July 19, 2020, 03:11:59 PM by Godra »