如何使用 DBLookupComboBox 在 Delphi 中的表单页面之间移动
How can move between the forms pages in Delphi using DBLookupComboBox
我对 Delphi 中的 DBLookupComboBox 组件有疑问。我想使用 DBLookupComboBox 组件在 Delphi 程序中的表单页面之间移动。
例如,当我从 DBLookupComboBox 列表中选择名称“ahmed”时,它将移动到表格 4,而当我从 DBLookupComboBox 列表中选择名称“fatima”时,它将移动到表格 5,如图所示下面。
Delphi中此操作的适当代码是什么?
这是一个简单的代码来完成您的要求。在此代码中,我使用 TClientDataset 来存储数据以填充 TComboBox。当然你可以使用你喜欢的数据集。请注意,我使用的是 TCombobox 而不是 DBLookupCombobox。我觉得更好。
我使用 TDictionary link 名称和表格。必须将表单添加到项目和 auto-created。我在两个单位中创建了两种形式: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.
我对 Delphi 中的 DBLookupComboBox 组件有疑问。我想使用 DBLookupComboBox 组件在 Delphi 程序中的表单页面之间移动。
例如,当我从 DBLookupComboBox 列表中选择名称“ahmed”时,它将移动到表格 4,而当我从 DBLookupComboBox 列表中选择名称“fatima”时,它将移动到表格 5,如图所示下面。
Delphi中此操作的适当代码是什么?
这是一个简单的代码来完成您的要求。在此代码中,我使用 TClientDataset 来存储数据以填充 TComboBox。当然你可以使用你喜欢的数据集。请注意,我使用的是 TCombobox 而不是 DBLookupCombobox。我觉得更好。
我使用 TDictionary link 名称和表格。必须将表单添加到项目和 auto-created。我在两个单位中创建了两种形式: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.