在delphi中将值从表单1传递给form2 [英] Passing values from form 1 to form2 in delphi

查看:199
本文介绍了在delphi中将值从表单1传递给form2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将form1中的值分配给变量userid,name和privilege到form2。我怎么做?请帮助



I would like to pass the Values below in form1 that are assigned to the variables userid, name and privilege to form2 . How do i do that? please help

procedure TForm1.Button1Click(Sender: TObject);
var
 name,pasword, privilege:string;
     UserId: Integer;

begin

        IBQuery1.SQL.clear;
        IBQuery1.SQL.add('Select * from USER_ACCESS where USERID = '+ edit1.Text+'') ;
        IBQuery1.Active := true;
        UserId := IBQuery1.FieldByname('USERID').AsInteger;
        pasword:= IBQuery1.Fieldbyname('PASWORD').AsString;
     name := IBQuery1.fieldbyname('NAME').AsString;
        privilege := IBQuery1.fieldbyname('USERTYPE').AsString;
        if((UserId = StrToInt(edit1.Text)) And (pasword = edit2.Text))
         THEN
          begin
        ShowMessage('login Successful ' + name);
   Form2.Show;
   Form1.Hide;

        end

        else
        ShowMessage('Wrong Password');

        end;

推荐答案

在你的问题,表单类型在哪里,以及表单的实例在哪里都不太清楚。首先,避免命名如 TForm1 Form1 等等。给出所有语义名称。我假设你已经将 TForm2 的一些实例传递给 Form1 ,并且实例的名称与类型的名称相匹配(这在你的问题中并不明显;如果你使用自动生成名字,应该是这种情况,但你不应该使用这些名字。)



现在,你需要拥有 Form2 的适当成员才能接受您需要传递的值,并使这些成员可访问(公共)。然后就完成任务。使用表单实例的问题是:它会破坏类型封装。将实例传递给表单时,通常会传递过多的访问权限。真正强大的解决方案是为这种协作声明一个接口,并以其中一种形式实现它。然后,您可以将界面指针(引用)传递给其他表单,只通过界面成员(方法或属性)进行访问。



我希望你知道如何声明和实现接口。



-SA
In your question, it's not quite clear where is the form types, and where are the instances of the forms. First of all, avoid naming like TForm1, Form1, etc. Give everything semantic names. I assume you already passes some instance of TForm2 to Form1, and the names of the instances match the names of the types (this is not apparent from your question; if you use automatically generating names, it should be the case, but you should not use such names).

Now, you need to have appropriate member(s) of Form2 to accept the values you need to pass, and make those members accessible (public). Then just do the assignment. The problem of using the form instances is this: it compromises type encapsulation. When you pass an instance to the form to other form, you usually pass too much of access. The really robust solution is to declare an interface for such collaboration and implement it in one of the forms. Then, you can pass the interface pointer (reference) to other form, giving the access only through the interface members (methods or properties).

I hope you know how to declare and implement interfaces.

—SA


检查这两个:

将值从表单1传递到表单2 [ ^ ]

在表格之间传递变量 [ ^ ]
Check these two:
passing value from form 1 to form2[^]
Passing variables between forms[^]


这篇关于在delphi中将值从表单1传递给form2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆