一下就忘记怎么用了,记录一下吧
想要框里面默认为空,Miscellaneous的propertier属性的EmptySelection设置为空就好
用下拉框第一个数据来填充:cxCheckComboBoxArea.States[0] := cbsChecked;
清空此框:cxCheckComboBoxArea.clear;
实例:
注释:sSQLStr,所要查询的sql语句,cxCheckComboBo控件名字,sql语句查询出来的2个字段,前者为Key,后者为Value
过程:Button(Sender:Tobject);
FillcxComboboxItem(sSQLStr, cxCheckComboBoxArea, 'City', 'PostCode');//城市/邮编
过程实现:
procedure Tform1.Button(Sender:Tobject); var sSqlStr:string; begin sSqlStr:='select City,PostCode from CityInfo' ; FullcxComboboxItem(sSqlStr,cxCheckComboBoxArea,'City','PostCode'); //cxCheckComboBoxArea控件的名字
end;
procedure Tform1.FillcxComboboxItem(ASQLStr: string; ACmb: TcxCheckComboBox; AListFieldName, AKeyFieldName: string); var AdoQuery: TAdoquery; begin try AdoQuery := TAdoquery.Create(nil); //必须要创建类的实例 AdoQuery.Connection := dmMain.ConnClient; try AdoQuery.SQL.Clear; AdoQuery.SQL.Add(ASQLStr); AdoQuery.Open; if AdoQuery.RecordCount > 0 then begin ACmb.Properties.Items.Clear; AdoQuery.First; while not AdoQuery.Eof do begin with cxCheckComboBoxArea, Properties do begin with Items.Add do begin Description := AdoQuery.FieldByName(AListFieldName).AsString; //City,下拉列表所显示的内容 Tag := AdoQuery.FieldByName(AKeyFieldName).Value;//PostCode,City所代表邮编 end; end; AdoQuery.Next; end; ACmb.ItemIndex := 0; end; except Application.MessageBox('数据加载失败,请重新查询!', '提示', MB_OK + MB_ICONINFORMATION); end; finally AdoQuery.Connection := nil; AdoQuery.Free; end; end;
取这个City对应的邮编:
cnt := cxCheckComboBoxArea.Properties.Items.Count;//先取得列表有多少个City for idx := 0 to cnt - 1 do //循环判断哪些是选中,哪些是没选中的 begin if cbsChecked=cxCheckComboBoxArea.States[idx] then //如果是选中状态 begin Showmessage(IntToStr(cxCheckComboBoxArea.Properties.Items[idx].Tag)); //则显示City对应的Tag值也就是邮编 end; end;
设置此下拉框的是否全选或者全不选:
cnt := cxCheckComboBoxArea.Properties.Items.Count; for idx := 0 to cnt - 1 do begin cxCheckComboBoxArea.States[idx] := cbsUnchecked; //设置全都不选,cbsUnchecked他被定义在枚举类型里面,TcxCheckBoxState = (cbsUnchecked, cbsChecked, cbsGrayed);
end;
cxCheckComboBoxArea.Enabled := False;