Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass information between forms - slightly complicated situation

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
I have been requested to work on a program, adding a login form to it. With the gracious assistance of some posters here, I was able to get the login form to work, and to cause it to show up during the program's run. However, my boss also wants me to display some of the information (UserID, UserName [not the same; ID is a numeric value]) from the login as part of the main program.

I've looked into what I can find about variables, but I'm not seeing how I could do this with my current setup. As best I understand it, the login form appears to be dying at the end of its brief run. Therefore, since that run is called and completed with a single command, I do not appear to have any place where I can set a dpr-local variable equal to it; attempts to do so have resulted in either getting a value of '' for the string value I'm trying to send, or else getting a run-breaking error. For the record, both the login form and the main form are in the dpr file's 'uses' block, and the variable in the login form is in the interface segment. Again, this setup does not appear to work.

Is there any way I can successfully pass an actual variable value between the login form and the main form, or will I need to write the value to an outside file then quickly reread it?
 
While both forms are alive, you basically just do an assign:

Code:
MainForm.UserName.Text := LoginForm.UserName.Caption;

Or you can put an assign like above to a basic outside variable in the Destroy section of the first form and pick it back up and load it into the second form.

----------
Measurement is not management.
 
Here is the general process of what I do:

Code:
Form_Main:
uses
  form_Login:

function SuccessfulLogin: boolean;
var
  frmLogin: TfrmLogin //Create an instance of the TfrmLogin form
begin
  result := False; //default is to fail
  frmLogin.Create(Application); //create the login form object
  try
    try 
      frmLogin.SomePublicVariable := TheValue; //pass a variable to the login form
      if frmLogin.ShowModal <> mrOK then
        exit; //if the user didn't click OK then fail

      //ValidLogin will check the username and ID
      if ValidLogin(frmLogin.texUsername.text, frmLogin,texPassword.Text)=False then
      begin
        showmessage('Bad login.');
        exit;
      end;
      //if we are this far then return true
      result := True;
    except
      on e:exception do 
      begin
        showmessage('Some error message:'+e.message);
      end;
    end;
  finally
    frmLogin.Release;  //best practice is to release your form after use.
    frmLogin := nil; //not required since we're headed out of scope now.
  end;

  


Form_Login:
  Create your form as normal. Create public variables as part of the form declaration if you want pass data back and forth.



 
If you are using a datamodule that is used by both forms, store the data in a global variable called UserId and UserName. I use this approach to update the statusbar of my DB applications

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top