Analitcs

Pesquisar no conteúdo do blog.atlabs.com.br

sexta-feira, 23 de maio de 2014

DELPHI - ComboBox.AddItem(Item: String; AObject: TObject); - Adicionando objetos em um ComboBox

Reações: 


unit Unit3;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;
  Type
    TATLCboItem = Class (TObject)
    FID    : Integer;
    FValue : String;
    Procedure SetID(Value : Integer);
    Procedure SetValue(Value : String);
    Public
      Constructor Create(AID : Integer; AValue : String; AListObj : TList = Nil);
      Destructor Destroy; override;
      Class Procedure CreateObj(Cbo : TComboBox; AID : Integer; AValue : String; AListObj : TList = Nil);
      Class Function GetID(Cbo : TComboBox; SText : String) : Integer;
    Protected
      Property  ID    : Integer Read FID    Write SetId;
      Property  Value : String  Read FValue Write SetValue;
  End;
type
  TForm3 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    FCboList : TList;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form3: TForm3;

implementation
{$R *.dfm}

{ TATLCboItem }

constructor TATLCboItem.Create(AID: Integer; AValue: String; AListObj: TList);
begin
  FID    := AID;
  FValue := AValue;
  if AListObj <> Nil then
    AListObj.Add(Self);
end;

class procedure TATLCboItem.CreateObj(Cbo : TComboBox; AID: Integer; AValue: String; AListObj: TList);
var
  AObjCboItem : TATLCboItem;
begin
  AObjCboItem := TATLCboItem.Create( AID, AValue, AListObj );
  Cbo.AddItem(AValue, AObjCboItem);
end;

destructor TATLCboItem.Destroy;
begin
  inherited;
end;

class function TATLCboItem.GetID(Cbo: TComboBox; SText : String): Integer;
begin
  Result := TATLCboItem(Cbo.Items.Objects[Cbo.Items.IndexOf(SText)]).ID;
end;

procedure TATLCboItem.SetID(Value: Integer);
begin
  FID := Value;
end;

procedure TATLCboItem.SetValue(Value: String);
begin
  FValue := Value;
end;

procedure TForm3.ComboBox1Change(Sender: TObject);
begin
  ShowMessage('ID do item: ' + IntToStr(TATLCboItem.GetID(TComboBox(Sender), TComboBox(Sender).Text)));
end;

procedure TForm3.FormCreate(Sender: TObject);
var
  I : Integer;
begin
  FCboList := TList.Create;
  for I := 65 to 90 do
    TATLCboItem.CreateObj(Self.ComboBox1, I * 100, 'TESTE ' + Chr(I) + ' = ' + IntToStr(I * 100), FCboList );
end;
end.

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.

Max Gehringer