【问题标题】:How a TList can get the input value from a Frame's TEdit/TComboboxTList 如何从 Frame 的 TEdit/TCombobox 获取输入值
【发布时间】:2020-08-04 02:08:19
【问题描述】:

主窗体包含TTabControl,它可以动态创建一些选项卡。每当我添加一个新选项卡时,都会创建一个框架并将其添加到新选项卡中。最后,我会将所有这些TTabItem 保存到TList 中。

TForm1 = class(TForm)
  TabControl1: TTabControl;

procedure TForm1.AddNewTab;
var
  profileFrame :TProfileFrame;
begin
  profileFrame := TProfileFrame.Create(Self);

  //TabItem
  TabItem := TabControl1.Add();
  inc(tab_name_Count);
  tabItem.Text := tab_name_Count.ToString;
  //
  profileFrame.Parent := tabItem;
  tablist.Add(TabItem);
end;

这是我的框架:

TProfileFrame = class(TFrame)
 Name: TEdit;
 Gender: TComboBox;

最后,如何获取框架中的 (Name) 和 (Gender) 值,并在主窗体中打印出来?如果假设我创建了 4 个选项卡,每个选项卡都有自己的框架,我怎样才能从不同的框架中获取值?我对 Delphi 非常困惑和新手。

【问题讨论】:

  • 您可以将对主框架的引用传递到动态创建的框架中,或者将对新创建的框架的引用存储在主框架中(取决于您希望进行通信的方式)。
  • "tabcontrol", "frame", "add button", "name", "gender"... 为什么两个不同的帐户似乎在同一个项目上工作?相关:How to get the user input from the frame and save into txt file?

标签: delphi frame firemonkey tabcontrol tlist


【解决方案1】:

主要问题是你的框架变量是过程局部变量。

我看到了解决您问题的不同方法。

首先:使用TObjectList

uses ..., System.Generics.Collections;

TForm1 = class(TForm)
  TabControl1: TTabControl;
private
  FFrames:TObjectList<TProfileFrame>;    

procedure TForm1.AddNewTab;
var
  profileFrame :TProfileFrame;
begin
  //TabItem
  TabItem := TabControl1.Add();
  profileFrame := TProfileFrame.Create(TabItem); 
  inc(tab_name_Count);
  tabItem.Text := tab_name_Count.ToString;
  profileFrame.Parent := tabItem;
  if not assigned(FFrames) then
    FFrames := TObjectList<TProfileFrame>.Create(false); //we don't need ObjectList to own Frame, I suppose, so we have to pass `false` into Create method
  FFrames.Add(profileFrame);
  tablist.Add(TabItem);
end;

//Just to demonstrate how to get value from frame
function TForm1.GetGenderFromFrame(ATabItem:TTabItem):String;
var i:integer;
begin
  result := '';
  if FFrames.Count > 0 then
  for i := 0 to FFrames.Count - 1 do
    if FFrames[i].TabItem = ATabItem then
    result := FFrames[i].Gender.Selected.Text;
end;

或者您可以使用其他方式(在 Delphi 10.1 FMX 项目中检查)。您必须像这样更改您的程序:

procedure TForm1.AddNewTab;
var
  profileFrame :TProfileFrame;
begin  
  //TabItem
  TabItem := TabControl1.Add();
  profileFrame := TProfileFrame.Create(TabItem);
  inc(tab_name_Count);
  tabItem.Text := tab_name_Count.ToString;
  //
  profileFrame.Parent := tabItem;
  tablist.Add(TabItem);
end;

现在您的框架拥有所有者:TabItemTabItem 有组件。我们可以使用它:

function TForm1.GetGenderFromFrame(ATabItem:TTabItem):String;
var i:integer;
begin
  result := '';
  if ATabItem.ComponentCount > 0 then
  for i := 0 to ATabItem.ComponentCount - 1 do
    if ATabItem.Components[i] is TProfileFrame then
    result := (ATabItem.Components[i] as TProfileFrame).Gender.Selected.Text;
end;

附:您可以使用for ... in ... do 代替for ... to ... do,它可能会更好,但这取决于您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2018-05-26
    相关资源
    最近更新 更多