如何从动态生成的表单中检索数据?

发布于 2024-12-12 02:42:12 字数 1566 浏览 0 评论 0原文

我在从程序动态生成的表单中获取信息时遇到问题。

我的程序一直运行,直到需要用户提供数据为止;此时它应该运行一个输入表单,并从中检索数据。

下面的示例代码运行并显示输入表单,但不允许对其进行任何输入。它确实返回表单初始化时编辑框中文本的长度。

这种方法有什么不正确的地方?

如果可能的话,我想保持分离接口和实现(包括表单)的整体方法;我还想继续使用无模式的表单显示。

编辑

按照@Ken White的建议对模态形式进行调查后,我确定模态形式不适合我想要做的事情。我想出了一个解决方案,总结为:

  1. 两个接口:
  2. IResult - 定义为将所需的数据从表单传递到 UserInput。
  3. IUserInput - 从 IResult 中的表单获取用户输入。
  4. 两个创建者函数 - 每个接口一个。
  5. 接口的实现 - 每个接口一个对象,一个用于实际的用户输入表单。

我写了详细说明 该解决方案。

结束编辑

program TestProject2;

uses Dialogs, Forms, StdCtrls, SysUtils;

type
  ITestForm = interface
    function getFormString: string;
    procedure setFormString(aString: string);
  end;

  TForm6 = class(TForm, ITestForm)
    Edit1: TEdit;
  private
    function getFormString: string;
    procedure setFormString(aString: string);
  end;

{$R Unit6.dfm}

function NewTestFormOutput: string;
  var
    tmpForm: TForm6;
  begin
    Application.CreateForm(TForm6, tmpForm);
    tmpForm.Show;
    Result := tmpForm.getFormString;
  end;

function TForm6.getFormString: string;
  begin
    Result := Edit1.Text;
  end;

procedure TForm6.setFormString(aString: string);
  begin
    Edit1.Text := aString;
  end;

var
  i: Integer;
  parser: string;
begin
  parser := NewTestFormOutput;
  i := Length(parser);
  ShowMessage('The length is: ' + InttoStr(i));
end.

I am having problems getting information out of forms that have been dynamically generated by the program.

My program runs until it needs data from the user; at which point it should run an input form, and retrieve data from it.

The below example code runs and displays the input form, but does not allow any input to it. It does return the length of the text in the edit box at initialisation of the form.

What is incorrect in this approach?

If possible I want to maintain the overall approach of seperating interfaces and implementation (including forms); I also want to continue to use modeless display of forms.

EDIT

After investigation of modal forms as suggested by @Ken White, I determined that modal forms were not appropriate for what I was trying to do. I came up with a solution, summarized as:

  1. Two interfaces:
  2. IResult - defined to carry the required data from the form to UserInput.
  3. IUserInput - obtain user input from a form in an IResult.
  4. Two creator functions - one for each Interface.
  5. Implementation of the interfaces - one object for each interface, and one for the actual user input form.

I have written a detailed description of this solution.

END EDIT

program TestProject2;

uses Dialogs, Forms, StdCtrls, SysUtils;

type
  ITestForm = interface
    function getFormString: string;
    procedure setFormString(aString: string);
  end;

  TForm6 = class(TForm, ITestForm)
    Edit1: TEdit;
  private
    function getFormString: string;
    procedure setFormString(aString: string);
  end;

{$R Unit6.dfm}

function NewTestFormOutput: string;
  var
    tmpForm: TForm6;
  begin
    Application.CreateForm(TForm6, tmpForm);
    tmpForm.Show;
    Result := tmpForm.getFormString;
  end;

function TForm6.getFormString: string;
  begin
    Result := Edit1.Text;
  end;

procedure TForm6.setFormString(aString: string);
  begin
    Edit1.Text := aString;
  end;

var
  i: Integer;
  parser: string;
begin
  parser := NewTestFormOutput;
  i := Length(parser);
  ShowMessage('The length is: ' + InttoStr(i));
end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-12-19 02:42:12

您发布的代码不允许用户在检索 TForm6.Edit1.Text 的内容之前提供任何输入。您基本上会说:

  1. 创建表单
  2. 将其显示给用户
  3. 立即读取设计时在表单的 Edit1.Text 中设置的任何内容

如果您想从用户那里获取内容,您必须让他们有机会将其提供给您。您需要使用 ShowModal 来呈现表单并让用户输入内容,或者使用 Show 并让表单传回信息(通过诸如 PostMessage 之类的机制) 或事件处理程序,正如 Gerry 在他的评论中提到的那样)当用户单击按钮时让您知道有数据要检索。

但要点仍然是,您必须让用户有机会提供输入,然后才能检索它。

Your code as you posted it doesn't allow the user to provide any input before it retrieves the content of TForm6.Edit1.Text. You basically say:

  1. Create the form
  2. Show it to the user
  3. Immediately read whatever was set at designtime in the form's Edit1.Text

If you want to get content from the user, you have to give them a chance to give it to you. You need to either use ShowModal to present the form and let the user enter content, or use Show and have the form pass back information (via some mechanism like PostMessage or an event handler, as Gerry mentioned in his comment) when the user clicks a button to let you know there's data to retrieve.

The point is still, though, that you have to allow the user a chance to provide input before you can retrieve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文