DBLookupCombobox實現下拉聯動
阿新 • • 發佈:2018-08-20
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建表及數據SQLGO 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 ONGO 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 (N‘c1001‘, N‘北京市‘, N‘1001‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1002‘, N‘上海市‘, N‘1001‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1003‘, N‘天津市‘, N‘1001‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1004‘, N‘重慶市‘, N‘1001‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1005‘, N‘廣州市‘, N‘1002‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1006‘, N‘深圳市‘, N‘1002‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1007‘, N‘珠海市‘, N‘1002‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1008‘, N‘南寧市‘, N‘1003‘, NULL) GO INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (N‘c1009‘, N‘玉林市‘, N‘1003‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1001‘, N‘越秀區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1002‘, N‘荔灣區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1003‘, N‘天河區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1004‘, N‘海珠區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1005‘, N‘黃浦區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1006‘, N‘蘿崗區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1007‘, N‘芳村區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1008‘, N‘白雲區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1009‘, N‘花都區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1010‘, N‘增城區‘, N‘c1005‘, NULL) GO INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (N‘d1011‘, N‘從化區‘, N‘c1005‘, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N‘1001‘, N‘直轄市‘, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N‘1002‘, N‘廣東省‘, NULL) GO INSERT [dbo].[province] ([code], [name], [other]) VALUES (N‘1003‘, N‘廣西壯族自治區‘, NULL) GO
窗體大概放這些控件,都是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實現下拉聯動