标签:arch src 打开 数据 enable inf 代码 div tab
这次用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 (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;
标签:arch src 打开 数据 enable inf 代码 div tab
原文地址:https://www.cnblogs.com/Kwong/p/9503315.html