Einzelnen Beitrag anzeigen

MiniMatrix

Registriert seit: 5. Feb 2010
16 Beiträge
 
#3

AW: TComboBox mit Aliaswerten

  Alt 31. Jul 2014, 13:38
Nunja, die Idee mit AddObject hatte ich auch schon, aber da fehlt mir eine Menge an "Komfort" und es müsste noch einiges gebaut werden, andernfalls müsste dann jedesmal wieder was gebaut werden.
Eine Komponente, die einfach zu verwenden wäre, ist da schon eher mein Ding. Leider stecke ich in der VCL nicht so wirklich tief drin (Delphi 2007) und tue mir da schwer.

Trotzdem habe ich einmal einen absolut nicht funktionierenden "Rumpf" gebaut. Jeder Versuch der Klasse ein Item oder eine Liste hinzuzufügen scheitert. Das erste Problem hierbei scheint wohl, das die Items vom Type TStrings sind und nicht vom Typ TStringList (wie ich erwartet hätte. Wo sind die dann gespeichert?) - also keine Werte selbst halten.
Die zweite Schwierigkeit ist, das Objekte, die der Liste hinzugefügt werden, auch selbst wieder gelöscht werden müssen (laut Doku).

Delphi-Quellcode:
unit AliasComboBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls;

type
  TAliasComboBox = class( TCustomComboBox )
  private
    { Private-Deklarationen }
  protected
    { Protected-Deklarationen }
    function GetItemsClass: TCustomComboBoxStringsClass; override;
    procedure SetItems( const Value: TStrings ); override;
    function GetItemAlias: String;
    procedure SetItemAlias( sAlias: String );
  public
    { Public-Deklarationen }
  published
    { Published-Deklarationen }
    property Align;
    property AutoComplete default True;
    property AutoCompleteDelay default 500;
    property AutoDropDown default False;
    property AutoCloseUp default False;
    property BevelEdges;
    property BevelInner;
    property BevelKind default bkNone;
    property BevelOuter;
    property Style; {Must be published before Items}
    property Anchors;
    property BiDiMode;
    property CharCase;
    property Color;
    property Constraints;
    property Ctl3D;
    property DragCursor;
    property DragKind;
    property DragMode;
    property DropDownCount;
    property Enabled;
    property Font;
    property ImeMode;
    property ImeName;
    property ItemAlias: String read GetItemAlias write SetItemAlias;
    property ItemHeight;
    property ItemIndex default -1;
    property MaxLength;
    property ParentBiDiMode;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
    property OnChange;
    property OnClick;
    property OnCloseUp;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDrawItem;
    property OnDropDown;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMeasureItem;
    property OnMouseEnter;
    property OnMouseLeave;
    property OnSelect;
    property OnStartDock;
    property OnStartDrag;
    property Items; { Must be published after OnMeasureItem }
  end;//of class

procedure Register;

implementation

//==============================================================================
procedure Register;
begin
  RegisterComponents( 'Beispiele', [ TAliasComboBox ]);
end;//of procedure

//======================================================================== Types
type
  TAliasComboBoxStrings = class( TCustomComboBoxStrings )
  public
    procedure Delete(Index: Integer); override;
  end;//of class

type
  TAlias = class( TObject )
  private
    { Private-Deklarationen }
    FAlias: String;
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor CreateAlias( sAlias: String ); virtual;
  published
    { Published-Deklarationen }
    property Alias: String read FAlias;
  end;//of class


//======================================================== TAliasComboBoxStrings
procedure TAliasComboBoxStrings.Delete( Index: Integer );
var
  aObj: TObject;
begin
  if( 0 > Index )
  then //Do nothing
  else begin
       //Löschen des erzeugten Objektes
       aObj:= GetObject( Index );
       aObj.Free;
       aObj := nil;
       PutObject( Index, aObj );
       end;//of if

  inherited Delete( Index );
end;//of procedure

//======================================================================= TAlias
constructor TAlias.CreateAlias( sAlias: String );
begin
  inherited Create();

  FAlias := sAlias;
end;//of constructor

//=============================================================== TAliasComboBox
function TAliasComboBox.GetItemsClass: TCustomComboBoxStringsClass;
begin
  Result := TAliasComboBoxStrings;
end;//of function

//------------------------------------------------------------------------------
procedure TAliasComboBox.SetItems( const Value: TStrings );
var
  iCount: Integer;
begin
  inherited SetItems( Value );

  if( not Assigned( Value ))
  then //Do nothing
  else if( 0 >= Value.Count )
  then //Do nothing
  else begin
       for iCount := 0 to Value.Count - 1 do
         begin
         Items.Objects[ iCount ]:= TAlias.CreateAlias( Trim( Value.ValueFromIndex[ iCount ]));
         Items.Strings[ iCount ]:= Trim( Items.Names[ iCount ]);
         end;//of for
       end;//of else
end;//of procedure

//------------------------------------------------------------------------------
function TAliasComboBox.GetItemAlias: String;
begin
  if( 0 > ItemIndex )
  then Result := ''
  else Result := TAlias( Items.Objects[ ItemIndex ]).Alias;
end;//of function

//------------------------------------------------------------------------------
procedure TAliasComboBox.SetItemAlias( sAlias: String );
var
  bDoChg: Boolean;
  iCount: Integer;
begin
  if( 0 > ItemIndex )
  then bDoChg:= True
  else if( sAlias <> TAlias( Items.Objects[ ItemIndex ]).Alias )
  then bDoChg:= True
  else bDoChg:= False;

  if( bDoChg )
  then begin
       for iCount := 0 to Items.Count - 1 do
         begin
         if( 0 = CompareText( sAlias, TAlias( Items.Objects[ ItemIndex ]).Alias ))
         then begin
              ItemIndex := iCount;
              Text := Items[ iCount ];
              Break;//of for
              end;//of for
         end;//of for
       end;//of if
end;//of procedure

end.
  Mit Zitat antworten Zitat