Created On:  13 July 2012

Problem:

When user clicks a button on a main form I want to display a login form where the user can enter username and password information and then return back to the main form.

How can I return these login parameters back to the main form?

This is a .NET Windows Forms application.

Resolution:

For the example given here, displaying a login form and then getting username and password back to the main form could be handled as follows:

1. Make the username and password properties of the login form.
2. Display the login form using ShowDialog method
3. If OK is pressed on login form then set the properties with username and password and return DialogResult::OK to the main form.
4. If Cancel is pressed on login form then return DialogResult::Cancel to the main form.
5. When main form gets control it can read the properties from LoginForm and process them if OK was pressed.

An example project has been attached and appears below:

MainForm:
*-------------------------------------------------------------------------------------------------*
*                           Retrieving User Info from Login Form                                  *
*                                                                                                 *
* This example demonstrates how to display a login form from within a main form in order to get   *
* User name and Password back into main form.                                                     *
*                                                                                                 *
* In this example the LoginName and LoginPassword are implemented as properties within the login  *
* form. The LoginForm is displayed using ShowDialog which will display it as modal and will set   *
* a the DialogResult property of the form depending on whether the user pressed OK or Cancel. If  *
* OK was pressed then the main form will read the properties from the Login Form and precess them *
* accordingly.                                                                                    *
*-------------------------------------------------------------------------------------------------*
class-id testpass.Form1 is partial
inherits type System.Windows.Forms.Form.
working-storage section.
method-id NEW.
procedure division.
   invoke self::InitializeComponent
   goback.
end method.

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.
end class.

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.