使用另一种形式的控件 [英] Use Controls Of Another Form

查看:158
本文介绍了使用另一种形式的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Lazarus的项目,它有两个表单, FormMain OutputForm 。我想在第二个Form上用 code> OutputMemo 显示输出:

  procedure FormMain.ShowButton(Object:Sender); 
开始
,如果SaveDialog1.Execute然后
AProcess:= TProcess.Create(nil);
AProcess.CommandLine:='gcc.exe'+ SaveDialog1.FileName +'-o'TextField23.Text;
AProcess.Options:= AProcess.Options + [poWaitOnExit,poUsePipes];
AProcess.Execute;

OutputForm.OutputMemo.Lines.BeginUpdate;
//OutputForm.OutputMemo.Lines.Clear;
OutputForm.OutputMemo.Lines.LoadFromStream(AProcess.Output);
OutputForm.OutputMemo.Lines.EndUpdate;

AProcess.Free;
end;

但是当我尝试编译这段代码时,我得到了错误:


未找到标识符OutputForm

OutputForm单元的顶部我有:

 单位输​​出; 

当我尝试从FormMain单元调用它时( OutputForm:Output;


定义类型错误


>

我需要做什么?

解决方案

RRUZ表示,引用声明OutputForm的单元。这里有一个基本的想法:

每个表单都有一个表单声明文件(Delphi中的DFM;我认为Lazarus称之为LFM)和一个相应的Object Pascal单元文件(.PAS)你把他们的代码放在哪里。就编译器而言,这是一个普通的单元文件。唯一的区别是它有一个与之关联的表单。



打开OutputForm的代码并查看顶部。它会说类似单元OutputForm;复制单元名称,并将其粘贴到FormMain单元的 uses 子句中,然后它应该可以工作。
$ b 编辑:不太清楚你想要做的修改,但是你不需要重新声明OutputForm。它应该已经在输出单元中声明为全局变量。你只需要将Output添加到你的uses子句中,所以你最终会得到类似的结果:

  unit Main; 

接口

使用
Windows,消息,SysUtils,类,图形,控件,表单,对话框,
输出; //在单独的行上显示它不是系统库

类型
TFrmMain = class(TForm)
...


I have a project on Lazarus that have two Forms, FormMain and OutputForm. I want to show a output on OutputMemo at the second Form with this code:

procedure FormMain.ShowButton(Object: Sender);
begin 
  if SaveDialog1.Execute then 
    AProcess := TProcess.Create(nil); 
  AProcess.CommandLine := 'gcc.exe ' + SaveDialog1.FileName + ' -o ' TextField23.Text; 
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes]; 
  AProcess.Execute; 

  OutputForm.OutputMemo.Lines.BeginUpdate; 
  //OutputForm.OutputMemo.Lines.Clear; 
  OutputForm.OutputMemo.Lines.LoadFromStream(AProcess.Output); 
  OutputForm.OutputMemo.Lines.EndUpdate; 

  AProcess.Free; 
end;

But when I try to compile this code, I got the error:

Identifier not found "OutputForm"

At the top of OutputForm unit I have:

unit Output;

And when I try to call it from FormMain unit(OutputForm: Output;) I got this error:

Error in type definiition

What I have to do?

解决方案

As RRUZ said, you need a reference to the unit where OutputForm is declared. Here's the basic idea:

Each form has a form declaration file (DFM in Delphi; I think Lazarus calls them LFMs) and a corresponding Object Pascal unit file (.PAS) where you put their code. This is a normal unit file like any other, as far as the compiler's concerned. The only difference is that it has a form associated with it.

Open the code for OutputForm and look at the top. It'll say something like "unit OutputForm;" Copy the unit name, and paste it into the uses clause of FormMain's unit, and then it should work.

EDIT: Not quite sure what you're trying to do with that edit, but you don't need to redeclare OutputForm. It should already be declared as a global variable in the Output unit. You just need to add Output to your uses clause, so you'll end up with something similar to this:

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Output; //on a separate line to show it's not a system lib

type
  TFrmMain = class(TForm)
  ...

这篇关于使用另一种形式的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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