Fórum Componente Combo Box - Criando Propriedades #233339
20/05/2004
0
Como estao todos vcs??
Espero que bem.
O pro é o seguinte:
Consegui, aos trancos e barrancos, fazer um componente com duas combo boxes que mostra apenas as cidades do estado selecionado anteriormente. o negocio é que eu quero criar mais duas propriedades: data source: exibir todos os data sources e data fields: exibe os campos dos data source escolhidos. Se algúem souber obrigado e se quiser o componente para testar, bastar postar o e-mail
Valeu e boa tarde a todos
Dbergkamps
Curtir tópico
+ 0Posts
20/05/2004
Dbergkamps
alguem tem uma ideia
Gostei + 0
21/05/2004
Dbergkamps
Gostei + 0
21/05/2004
Dbergkamps
Gostei + 0
22/05/2004
Tatuweb
Vou mostrar abaixo um escopo de como criar as propriedades data source e data fields. Vc deve adaptar o código ao seu componente.
na cláusula private coloque:
private FDataLink: TFieldDataLink; function GetDataSource: TDataSource; function GetDataField: string; procedure SetDataSource (Value: TDataSource); procedure SetDataField (Value: string);
Na cláusula published:
property DataField: string read GetDataField write SetDataField; property DataSource: TDataSource read GetDataSource write SetDataSource;
No constructor do componente:
inherited Create (AOwner); FDataLink := TFieldDataLink.Create; FDataLink.Control := Self;
No destructor do componente
FDataLink.Free;
Implementando as declarações
function TSeuComponente.GetDataSource: TDataSource; begin Result := FDataLink.DataSource; end; procedure TSeuComponente.SetDataSource (Value: TDataSource); begin FDataLink.DataSource := Value; if Value <> nil then Value.FreeNotification (Self); end; procedure TSeuComponente.GetDataField: string; begin Result := FDataLink.FieldName; end; procedure TSeuComponente.SetDataField (Value: string); begin FDataLink.FieldName := Value; end;
Isso aí em cima é o básico. Observe que eu ´comi´ algumas declarações. Pode ser que seja necessário mexer em alguma coisa no código. Se tiver dúvida ou não ajudar é só postar!
Gostei + 0
22/05/2004
Dbergkamps
Mas continuo com um problema:
O componente é um panel com duas comboboxes. Como faco para uma receba o campo estado e a outra cidade?????
ou separa-las para carrega-las individualmente???/
Obriado cara...
Valeu mesmo .....
Olha o source do componenteunit UFCidade;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, DBClient, DBCtrls, DB;
type
TUFCidade = class(TCustomPanel)
private
FArquivo : String;
Fuf : String;
FCidade : String;
Flbuf : Tlabel;
Flbcidade : Tlabel;
FCombouf : TComboBox;
FCombocidade: TCombobox;
FClient : TClientDataSet;
FDataLink: TFieldDataLink;
function GetDataSource: TDataSource;
function GetDataField: string;
procedure SetDataSource (Value: TDataSource);
procedure SetDataField (Value: string);
{ Private declarations }
protected
procedure ProcessaCidade(Sender: TObject);
Procedure ProcessaUF(Sender: TObject);
procedure SaidaUF(Sender: TObject);
procedure SaidaCidade(Sender: TObject);
procedure Tamanho(Sender: tobject);
{ Protected declarations }
public
Constructor Create(AOwner: TComponent); override; Destructor Destroy; override;
{ Public declarations }
published
property Arquivo_XML : String Read FArquivo Write FArquivo;
property UF : String Read FUF Write FUF;
property Cidade : String Read FCidade write Fcidade;
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
{ Published declarations }
end;
procedure Register;
implementation
constructor TUFCidade.Create(Aowner: TComponent);
begin
inherited Create(Aowner);
// Panel ...
Height := 037;
Width := 263;
BevelInner := bvnone;
bevelouter := bvnone;
Caption := ´´;
Font.Name:=´Tahoma´;
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
// Label Uf ...
Flbuf := Tlabel.Create(Self);
Flbuf.Caption := ´UF´;
Flbuf.Left := 2;
Flbuf.Top := 2;
Flbuf.parent:=self;
// Label Cidade ...
FLbCidade := TLabel.Create(Self);
FLbCidade.Caption := ´Cidade´;
FLbCidade.Left := 44;
FLbCidade.Top := 02;
FLbCidade.Parent := Self;
// Combo UF ...
FComboUf := TComboBox.Create(Self);
FComboUf.Cursor := crHandPoint;
FComboUF.Sorted := True;
FComboUf.Left := 02;
FComboUF.Top := 14;
Fcombouf.height:= 21;
Fcombouf.width := 39;
Fcombouf.TabStop:= true;
FCombouf.Sorted:=TRUE;
FCombouf.Style:=csDropDownList;
FCombouf.Font.Name:=´Tahoma´;
FCombouf.onenter:=ProcessaUF;
Fcombouf.OnExit:=SaidaUF;
FCombouf.parent:=self;
// combo cidade ...
Fcombocidade := Tcombobox.create(self);
FCombocidade.Cursor:= crHandPoint;
FCombocidade.Left:= 44;
Fcombocidade.top:= 14;
Fcombocidade.Height:=21;
FCombocidade.Style:=csDropDownList;
FCombocidade.Width:=self.Width - 45;
FCombocidade.TabStop:=true;
FCombocidade.Font.Name:=´Tahoma´;
FCombocidade.Sorted:=TRUE;
FCombocidade.OnEnter:=ProcessaCidade;
FCombocidade.OnExit:=SaidaCidade;
FCombocidade.Parent:=self;
// Clientdataset ...
FClient :=TClientDataSet.Create(Self);
// Ajustes do Componente...
OnResize:= Tamanho;
end;
procedure Register;
begin
RegisterComponents(´UFCidade´, [TUFCidade]);
end;
procedure TUFCidade.ProcessaUF(Sender: TObject);
begin
with fcombouf do
begin
items.clear;
Items.Add(´AC´);Items.Add(´AM´);Items.Add(´MA´);
Items.Add(´BA´);Items.Add(´DF´);Items.Add(´CE´);
Items.Add(´SP´);Items.Add(´RJ´);Items.Add(´RR´);
Items.Add(´RO´);Items.Add(´GO´);Items.Add(´RS´);
Items.Add(´SE´);Items.Add(´ES´);Items.Add(´PA´);
Items.Add(´PR´);Items.Add(´TO´);Items.Add(´PI´);
Items.Add(´RN´);Items.Add(´PB´);Items.Add(´PE´);
Items.Add(´AL´);Items.Add(´MG´);Items.Add(´MT´);
items.Add(´SC´);Items.Add(´MS´);
if FUF <> ´´ then
Itemindex:= Items.Indexof(FUF);
end;
end;
procedure TUFCidade.SaidaUF(Sender: TObject);
var
Fachou: Boolean;
begin
Fcombouf.ItemIndex:=FCombouf.Items.IndexOf(Uppercase(FCombouf.text));
Fachou:=FCombouf.ItemIndex >=0;
if not fachou then
begin
MessageDlg(´UF Inválida. Não Encontrada´, mtInformation, [mbok], 0);
Fcombouf.text:=´´;
Fcombouf.SetFocus;
exit;
end;
Fcombouf.text:=Uppercase(Fcombouf.text);
Fuf:=Fcombouf.text;
end;
procedure TUFCidade.SaidaCidade(sender: TObject);
var
Fachou: Boolean;
begin
FCombocidade.ItemIndex:=FCombocidade.Items.Indexof(UpperCase(Fcombocidade.text));
Fachou:=(FCombocidade.itemindex>=0);
if not Fachou then
begin
Messagedlg(´Cidade não econtrada!´, mtInformation, [Mbok],0);
FCombocidade.text:=´´;
FCombocidade.setfocus;
exit;
end;
Fcombocidade.text:=UpperCase(Fcombocidade.text);
Fcidade:=Fcombocidade.text;
end;
procedure TUFCidade.Tamanho(Sender: TObject);
begin
Fcombouf.width:=39;
Fcombouf.left:=02;
Fcombocidade.width:=Self.width-45;
Fcombocidade.left:=Fcombouf.left+42;
Self.height:=37;
end;
function TUFCidade.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TUFCidade.SetDataSource (Value: TDataSource);
begin
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification (Self);
end;
function TUFCidade.GetDataField: string;
begin
Result:=FDataLink.FieldName;
end;
procedure TUFCidade.SetDataField (Value: string);
begin
FDataLink.FieldName := Value;
end;
procedure TUFCidade.ProcessaCidade(sender: Tobject);
begin
if Trim(Farquivo)= ´´ then
begin
MessageDlg(´Informe o Arquivo XML a Processar´,mtInformation,[mbok], 0);
exit;
end; // if Trim(varquivo) = ´´ then ...
try
FClient.Filtered:= false;
FClient.FileName:= FArquivo;
FClient.filter:= ´UF = ´´´ + FCombouf.text + ´´´´;
FClient.Filtered:=true;
FClient.open;
except
MessageDlg(´Não foi possível abrir o arquivo XML!´, mtError, [MBok], 0);
exit;
end;
with FCombocidade do
begin
Items.BeginUpdate;
if FCidade = ´´ then text := ´´;
items.clear;
while not FClient.eof do
begin
items.add(Fclient.fieldbyname(´CIDADE´).Asstring);
FClient.next;
end;
FClient.close;
Items.EndUpdate;
If FCidade <> ´´ then
Itemindex:= Items.indexof(fcidade);
end;
end;
destructor TUFCidade.Destroy;
begin
Flbuf.free;
Flbcidade.free;
FCombouf.free;
FCombocidade.Free;
FClient.free;
FDataLink.Free;
inherited Destroy;
end;
end.
Gostei + 0
22/05/2004
Tatuweb
Gostei + 0
26/05/2004
Dbergkamps
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)