Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How to handle multiple forms in a Windows Forms Application

How to handle multiple forms in a Windows Forms Application

Attachment: MultiFormsDemo.zip

In a .NET WinForm application, a form is simply an instance of the System.Windows.Forms.Form class so it may be manipulated like any other .NET object.

When you first create a Windows Form Application a main form named Form1 is automatically added to the project and it is instantiated in the main.cbl program which is the entry point of the project.

Other forms can be added by right clicking on the project name in Solution Explorer add selecting Add->New item->Windows Form.

You can then instantiate and manipulate these new forms from your main form.

Please download the attachment MultiFormsDemo.zip from this article and unzip it onto your C:\drive retaining the folder structure in the .zip file.

Using Visual COBOL open up the solution C:\MultiFormsDemo\MultiFormsDemo\MultiFormsDemo.sln.

Press F5 to run the demo.
-----------------------------------------

The following is the source code for the main form.

 

 

      *-------------------------------------------------------------------------*
      *                        MultiForms Demo                                  *
      *                                                                         *
      *  This example demonstrates how multiple Windows forms can be used within*
      *  a .NET WinForm application.                                            *
      *                                                                         *
      *  The main form Form1 is created in main.cbl. Form1 contains a number of *
      *  button controls which allow you to create two additional forms called  *
      *  Form2 and Form3. Once these forms are created you can click additional *
      *  buttons to control whether they sould be hidden or shown.              *
      *                                                                         *
      *  An event handler is added to Form1 in order to capture the FormClosed  *
      *  event for both Form2 and Form3 so that Form1 can enable and disable the*
      *  correct buttons to allow for a new instance of Form2 or Form3 to be    *
      *  created. The FormClose event is triggered by clicking on the Close Form*
      *  button on Form2 or Form3.                                              *
      *-------------------------------------------------------------------------* 
       class-id MultiFormsDemo.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       01 Form2    type MultiFormsDemo.Form2.
       01 Form3    type MultiFormsDemo.Form3.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id btnCreate2_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           set Form2 to new type MultiFormsDemo.Form2
           
      *>  The following adds an event handler to this form which will catch the FormClosed event on Form2     
           invoke Form2::add_FormClosed(new System.Windows.Forms.FormClosedEventHandler(self::Form2_Closed))
           invoke Form2::Show
      *>  Disable the Create Form2 button so only one instance of Form2 will be created.     
           set self::btnCreate2::Enabled to false
           set self::btnHide2::Enabled to true
           set self::btnShow2::Enabled to true
       end method.

       method-id btnCreate3_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           set Form3 to new type MultiFormsDemo.Form3
           
      *>  The following adds an event handler to this form which will catch the FormClosed event on Form3     
           invoke Form3::add_FormClosed(new System.Windows.Forms.FormClosedEventHandler(self::Form3_Closed))
           invoke Form3::Show
      *>  Disable the Create Form3 button so only one instance of Form3 will be created.          
           set self::btnCreate3::Enabled to false
           set self::btnHide3::Enabled to true
           set self::btnShow3::Enabled to true
       end method.

       method-id btnHide2_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke Form2::Hide
       end method.

       method-id btnHide3_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke Form3::Hide
       end method.

       method-id btnShow2_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke Form2::Show
       end method.

       method-id btnShow3_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke Form3::Show
       end method.

       method-id Form1_Load final private.
       procedure division using by value sender as object e as type System.EventArgs.
      *> set the initial state of the buttons. 
           set self::btnCreate2::Enabled to true
           set self::btnHide2::Enabled to false
           set self::btnShow2::Enabled to false
           
           set self::btnCreate3::Enabled to true
           set self::btnHide3::Enabled to false
           set self::btnShow3::Enabled to false
                  
       end method.
      *> The following event handler will be triggered when the Close Form2 button is clicked in Form2 
       method-id Form2_Closed.
       procedure division using by value sender as object e as type System.Windows.Forms.FormClosedEventArgs.
           set self::btnCreate2::Enabled to true
           set self::btnHide2::Enabled to false
           set self::btnShow2::Enabled to false
       end method.
      *> The following event handler will be triggered when the Close Form3 button is clicked in Form3
       method-id Form3_Closed.
       procedure division using by value sender as object e as type System.Windows.Forms.FormClosedEventArgs.
           set self::btnCreate3::Enabled to true
           set self::btnHide3::Enabled to false
           set self::btnShow3::Enabled to false
       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
          invoke self::Close
       end method.

       end class.

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Comments

Is it possible to pass data from form2 back to form1?  For example, from a login screen (form1), show form2 and ask for a password & password confirmation, then pass both back to form 1?

For the example that you give, displaying a login form and then getting username and password back you could handle as follows:

Make the username and password properties of the login form.

display the login form using ShowDialog method

If OK is pressed on login form then set the properties with username and password and return DialogResult::OK to the main form.

If Cancel is pressed on login form then return DialogResult::Cancel to the main form.

When main form gets control it can read the properties from LoginForm and process them

Mainform:

      method-id loginBtn_Click final private.

      01 loginform type testpass.LoginForm.

      procedure division using by value sender as object e as type System.EventArgs.

          set loginform to new testpass.LoginForm

      *> Show loginform as a modal dialog and determine if DialogResult = OK.

          if (loginform::ShowDialog = type DialogResult::OK)

             set label1::Text to "User=" & loginform::loginName & " pass=" & loginform::loginPassword

          else

             set label1::Text to "User pressed Cancel"

          end-if

       end method.

LoginForm:

      class-id testpass.LoginForm is partial

                inherits type System.Windows.Forms.Form.

      working-storage section.

      01 loginName       string property.

      01 loginPassword   string property.

      method-id NEW.

      procedure division.

          invoke self::InitializeComponent()

          goback.

      end method.

      method-id btnOK_Click final private.

      procedure division using by value sender as object e as type System.EventArgs.

          set self::DialogResult to type DialogResult::OK

          set self::loginName to txtUser::Text

          set self::loginPassword to txtPassword::Text

          invoke self::Close

      end method.

      method-id btnCancel_Click final private.

      procedure division using by value sender as object e as type System.EventArgs.

          set self::DialogResult to type DialogResult::Cancel

          invoke self::Close

      end method.

      end class.

Version history
Revision #:
1 of 1
Last update:
‎2012-06-21 16:58
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.