1. 程式人生 > >DBLookupCombobox實現下拉聯動

DBLookupCombobox實現下拉聯動

nbsp image 數據 text 打開 分享圖片 style cluster open

這次用DBLookupCombobox實現省份-城市-地區的下拉聯動,用以學習DBLookupCombobox的一些用法

效果圖:

技術分享圖片

首先建立數據表(數據表形式可以多種多樣:數據庫表、文本、xml等,最終能轉換成Dataset數據集就好,這裏使用數據庫表)

1.省份表

技術分享圖片

2.城市表

技術分享圖片

3.地區表

技術分享圖片

時間關系,就簡單做了幾個數據,只有廣東-廣州-地區是完整的數據

建表及數據代碼:

技術分享圖片
USE [his_mz]
GO
/****** Object:  Table [dbo].[City]    Script Date: 2018-8-19 23:04:00 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[City]( [code] [varchar](10) NOT NULL, [name] [varchar](50) NOT NULL, [province] [varchar](10) NOT NULL, [other] [varchar](50) NULL, CONSTRAINT [PK_City] PRIMARY KEY CLUSTERED ( [code] ASC )WITH (PAD_INDEX = OFF
, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[district] Script Date: 2018-8-19 23:04:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON
GO CREATE TABLE [dbo].[district]( [code] [varchar](10) NOT NULL, [name] [varchar](50) NOT NULL, [city] [varchar](10) NOT NULL, [other] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[province] Script Date: 2018-8-19 23:04:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[province]( [code] [varchar](10) NOT NULL, [name] [varchar](50) NOT NULL, [other] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1001, N北京市, N1001, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1002, N上海市, N1001, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1003, N天津市, N1001, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1004, N重慶市, N1001, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1005, N廣州市, N1002, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1006, N深圳市, N1002, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1007, N珠海市, N1002, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1008, N南寧市, N1003, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1009, N玉林市, N1003, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1001, N越秀區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1002, N荔灣區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1003, N天河區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1004, N海珠區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1005, N黃浦區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1006, N蘿崗區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1007, N芳村區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1008, N白雲區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1009, N花都區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1010, N增城區, Nc1005, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1011, N從化區, Nc1005, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1001, N直轄市, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1002, N廣東省, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1003, N廣西壯族自治區, NULL) GO
建表及數據SQL

窗體大概放這些控件,都是Delphi自帶的。(如果安裝了Ehlib包,用DBLookupComboboxEh效果更佳)

技術分享圖片

DBLookupCombobox的屬性設置及說明

技術分享圖片

ListSource:下拉列表的數據集

ListField:下拉列表要顯示的字段的名稱

KeyField:要獲得的字段值。例如廣東對應的記錄,1002,廣東省。如果廣東省被選擇了,那麽DBLookupCombobox.KeyValue的值就取字段name為廣東的那條記錄的code值(1002)

初始化數據集

技術分享圖片
procedure TForm2.FormCreate(Sender: TObject);
begin
  //初始化數據集信息
  qryProvince.Close;
  qryCity.Close;
  qryDistrict.Close;

  qryProvince.SQL.Text := select * from province;
  qryCity.SQL.Text := select * from city;
  qryDistrict.SQL.Text := select * from district;

  qryProvince.Open;
  qryCity.Open;
  qryDistrict.Open;

  //打開數據集
  qryProvince.Open;
  qryCity.Open;
  qryDistrict.Open;

  //初始化DBLookupcombobox
  qryCity.Filtered := False;
  qryCity.Filter := code=+QuotedStr(‘‘);
  qryCity.Filtered := True;

  qryDistrict.Filtered := False;
  qryDistrict.Filter := code=+QuotedStr(‘‘);
  qryDistrict.Filtered := True;
end;
初始化數據集

當省份選擇之後,觸發DBLookupCombobox的CloseUp事件

技術分享圖片
procedure TForm2.cbProvinceCloseUp(Sender: TObject);
var
  keycode: string;
begin
  if cbProvince.Text=EmptyStr then
    keycode := EmptyStr
  else
    keycode := cbProvince.KeyValue; //關鍵是keyvalue,keyvalue的值來自於keyField,可以在屬性面板設置keyFiled

  //使用filter過濾,只顯示Province=keycode的數據集
  qryCity.Filtered := False;
  qryCity.Filter := Province=+QuotedStr(keycode);
  qryCity.Filtered := True;

  //清除城市下拉框已選的值
  cbCity.KeyValue := ‘‘;
  cbDistrict.KeyValue := ‘‘;
end;
當省份選擇之後

當城市選擇之後

技術分享圖片
procedure TForm2.cbCityCloseUp(Sender: TObject);
var
  keycode: string;
begin
  if cbCity.Text=EmptyStr then                
    keycode := EmptyStr
  else
    keycode := cbCity.KeyValue;
  //使用filter過濾,只顯示city=keycode的數據集
  qryDistrict.Filtered := False;
  qryDistrict.Filter := city=+QuotedStr(keycode);
  qryDistrict.Filtered := True;
  //由於有些城市的區域為空,所以要把DBLookupCombobox的enable設置FALSE,否則會報錯
  if qryDistrict.RecordCount=0 then
    cbDistrict.Enabled := False
  else
    cbDistrict.Enabled := True;

  //清除已選擇的地區的值
  cbDistrict.KeyValue := ‘‘;
end;
城市選擇之後

DBLookupCombobox實現下拉聯動