如何使用DBLookupComboBox在Delphi中的表单页面之间移动

问题描述

我对Delphi中的DBLookupComboBox组件有疑问。我想使用DBLookupComboBox组件在Delphi程序的表单页面之间移动。

例如,当我从DBLookupComboBox列表中选择名称“ ahmed”时,它将移至表格4;当我从DBLookupComboBox列表中选择名称“ fatima”时,它将移至表格5,如下图所示。

在Delphi中进行此操作的合适代码是什么?

enter image description here

解决方法

这是执行您要求的简单代码。在此代码中,我使用TClientDataset存储数据以填充TComboBox。当然,您可以使用自己喜欢的数据集。请注意,我使用的是TCombobox,而不是DBLookupCombobox。我认为这更好。

我使用TDictionary链接名称和表格。表单必须添加到项目中并自动创建。我在两个单元中创建了两种形式:UnitAhmed和UnitMohamed。这些只是重命名并保存在其单元中的空表单。

unit Unit6;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Data.DB,Datasnap.DBClient,Vcl.DBCtrls,System.Generics.Collections,UnitAhmedForm,UnitMohamedForm;

type
  TMainForm = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    ClientDataSet1 : TClientDataset;
    FDictionary : TDictionary<String,TForm>;
    procedure SelectedFormClose(Sender: TObject; var Action: TCloseAction);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.ComboBox1Click(Sender: TObject);
var
    SelectedForm : TForm;
begin
    // Try to find the name selected in the combobox and the associated form
    if not FDictionary.TryGetValue(ComboBox1.Text,SelectedForm) then begin
        ShowMessage('Not found');
        Exit;
    end;
    // The name and form are found,make it visible
    SelectedForm.Visible := TRUE;
    // Assign an OnClose event to make mainform visible when the selected
    // form is closed
    SelectedForm.OnClose := SelectedFormClose;
    // Hide the main form
    Visible              := FALSE;
end;

procedure TMainForm.SelectedFormClose(Sender: TObject; var Action: TCloseAction);
begin
    // Make the main form visible
    Visible := TRUE;
end;


procedure TMainForm.FormCreate(Sender: TObject);
begin
    // Create an in-memory dataset with one single field
    ClientDataSet1 := TClientDataset.Create(Self);
    ClientDataSet1.FieldDefs.Add('Name',ftString,32);
    ClientDataSet1.IndexFieldNames := 'Name';
    ClientDataSet1.CreateDataSet;

    // Populate the dataset with two records
    ClientDataSet1.Open;
    ClientDataSet1.Append;
    ClientDataSet1.FieldByName('Name').AsString := 'ahmed';
    ClientDataSet1.Post;
    ClientDataSet1.Append;
    ClientDataSet1.FieldByName('Name').AsString := 'mohamed';
    ClientDataSet1.Post;

    // Fill the combobox with the just created dataset content
    ClientDataSet1.First;
    while not ClientDataSet1.Eof do begin
        ComboBox1.Items.Add(ClientDataSet1.FieldByName('Name').AsString);
        ClientDataSet1.Next;
    end;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
    FreeandNil(FDictionary);
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
    // Create the dictionary which associate a name and a form.
    // We cannot do that in the FormCreate because at that time the other
    // forms are not created yet (By auto-create mechanism)
    // Here in the OnShow event handler,all forms are already created
    FDictionary := TDictionary<String,TForm>.Create;
    FDictionary.Add('ahmed',AhmedForm);
    FDictionary.Add('mohamed',MohamedForm);
end;

end.