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 pass data from one form to another when the form is closed

How to pass data from one form to another when the form is closed

Problem:

Customer has a WPF application that has a main form that launches other forms.
When one of these other forms is closed they would like to be able to pass some data entered in the form back to the main form.

How can this be accomplished?

Resolution:

This can be done by creating a method within the main form that will be invoked automatically when one of the other forms is closed by attaching the method to the FormClosed event handler for the child forms.

If the data that was entered in the child forms is stored as a property then that data can be read from within the main forms method that is attached.

Here is an example that has a main program that displays a new form using the Show method and then attaches a local method to the FormClosed event so that it will be invokes automatically when the form is closed. That method is then used to read the property set in the child form and populate a TextBox on the main form with the text.

Example:

Here is the code for Window 1 which is in a WPF application project:

 

       class-id WPFWindows1.Window1 is partial
                 inherits type System.Windows.Window.

       working-storage section.
       01 mywindow2 type WPFWindows2.Window2.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent()
           goback.
       end method.

       method-id Window_Closed2.
       procedure division using by value sender as object e as type System.EventArgs.
          set self::txtParam::Text to mywindow2::myfield
       end method.

       method-id Button_Click.
       procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
           set mywindow2 to new WPFWindows2.Window2
          *> use this syntax in V2.1 
invoke mywindow2::add_Closed(new EventHandler(self::Window_Closed2)) *> use this syntax in V2.2
*>attach method self::Window_Closed2 to mywindow2::Closed invoke mywindow2::Show end method. end class.

and the code for Windows 2 which is in a WPF library:

       class-id WPFWindows2.Window2 is partial
                 inherits type System.Windows.Window.

       working-storage section.
       01 myfield  string property.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent()
           set self::txtParam::Text to "Enter Name"
           goback.
       end method.

       method-id Button_Click.
       procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
           set myfield to self::txtParam::Text
           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.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-10-25 17:27
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.