Einzelnen Beitrag anzeigen

MiniMatrix

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

AW: TComboBox mit Aliaswerten

  Alt 31. Jul 2014, 16:12
Der Tipp mit der Objektliste war super. Danke dafür.

Hier die Version 1.0 (Anmerkungen und Verbesserungen bitte hier posten):

Delphi-Quellcode:
unit AliasComboBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Contnrs;

type
  TAliasObj = class( TObject )
  private
    { Private-Deklarationen }
    FLabel: String;
    FValue: String;
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor CreateAlias( sLabel, sValue: String ); virtual;
  published
    { Published-Deklarationen }
    property Value: String read FLabel;
    property Alias: String read FValue;
  end;//of class

type
  TAliasComboBox = class( TCustomComboBox )
  private
    { Private-Deklarationen }
    FAliasList: TObjectList;
  protected
    { Protected-Deklarationen }
    function GetAliasForLabel( sLabel : String ): String;
    function GetLabelForAlias( sAlias : String ): String;
    function GetItemAlias : String;
    procedure SetItemAlias ( sAlias : String );
    procedure AddSingleItemVal( sValue : String );
    procedure AddItem ( Item : String;
                                      aObject: TObject ); override;
    procedure SetItems ( const Value : TStrings ); override;
  public
    { Public-Deklarationen }
    constructor Create ( aOwner : TComponent ); override;
    destructor Destroy; override;
    procedure Clear; override;
    procedure AddAliasItem ( sLabel : String;
                                      sValue : String ); virtual;
    procedure AddAliasStrings( aList : TStrings ); virtual;
  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

//==================================================================== TAliasObj
constructor TAliasObj.CreateAlias( sLabel, sValue: String );
begin
  inherited Create();

  FLabel:= sLabel;
  FValue:= sValue;
end;//of constructor

//=============================================================== TAliasComboBox
constructor TAliasComboBox.Create( aOwner: TComponent );
begin
  inherited Create( aOwner );

  FAliasList:= TObjectList.Create;
end;//of constuctor

//------------------------------------------------------------------------------
destructor TAliasComboBox.Destroy;
begin
  if( Assigned( FAliasList ))
  then FAliasList.Free;

  inherited Destroy;
end;//of destructor

//------------------------------------------------------------------------------
procedure TAliasComboBox.Clear;
begin
  if( Assigned( FAliasList ))
  then FAliasList.Clear;

  inherited Clear;
end;//of procedure

//------------------------------------------------------------------------------
procedure TAliasComboBox.AddAliasItem( sLabel, sValue: String );
begin
  if( Assigned( FAliasList ))
  then FAliasList.Add( TAliasObj.CreateAlias( sLabel, sValue ));
  Items.Add( sLabel );
end;//of procedure

//------------------------------------------------------------------------------
procedure TAliasComboBox.AddAliasStrings( aList: TStrings );
var
  iCount: Integer;
begin
  if( Assigned( FAliasList ))
  then FAliasList.Clear;
  Items.Clear;

  if( Assigned( aList ))
  then begin
       Items.BeginUpdate;

       for iCount := 0 to aList.Count - 1 do
         begin
         AddSingleItemVal( aList.Strings[ iCount ]);
         end;//of for

       Items.EndUpdate;
       end;//of if
end;//of procedure

//------------------------------------------------------------------------------
function TAliasComboBox.GetAliasForLabel( sLabel: String ): String;
var
  iCount: Integer;
  iPos : Integer;
begin
  Result := '';
  sLabel := Trim( sLabel );
  if( '' <> sLabel )
  then begin
       iPos := Pos( Items.NameValueSeparator,
                    sLabel );
       if( 0 < iPos )
       then sLabel := Trim( Copy( sLabel, 1, iPos - 1 ));
       Result := sLabel;
       if( Assigned( FAliasList ))
       then begin
            for iCount := 0 to FAliasList.Count - 1 do
              begin
              if( 0 = CompareText( sLabel, TAliasObj( FAliasList.Items[ iCount ]).FLabel ))
              then begin
                   Result:= TAliasObj( FAliasList.Items[ iCount ]).FValue;
                   Break;//of for
                   end;//of if
              end;//of for
            end;//of if
       end;//of if
end;//of function

//------------------------------------------------------------------------------
function TAliasComboBox.GetLabelForAlias( sAlias: String ): String;
var
  iCount: Integer;
begin
  Result := '';
  sAlias := Trim( sAlias );
  if( '' <> sAlias )
  then begin
       if( not Assigned( FAliasList ))
       then Result := sAlias
       else begin
            for iCount := 0 to FAliasList.Count - 1 do
              begin
              if( 0 = CompareText( sAlias, TAliasObj( FAliasList.Items[ iCount ]).FValue ))
              then begin
                   Result:= TAliasObj( FAliasList.Items[ iCount ]).FLabel;
                   Break;//of for
                   end;//of if
              end;//of for
            end;//of else
       end;//of if
end;//of function

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

//------------------------------------------------------------------------------
procedure TAliasComboBox.SetItemAlias( sAlias: String );
var
  bDoChg: Boolean;
  sLabel: String;
  iIndex: Integer;
begin
  if( 0 > ItemIndex )
  then bDoChg:= True
  else if( ItemAlias <> sAlias )
  then bDoChg:= True
  else bDoChg:= False;

  if( bDoChg )
  then begin
       iIndex:= Items.IndexOf( GetLabelForAlias( sAlias ));
       if( 0 > iIndex )
       then //Do nothing
       else begin
            ItemIndex := iIndex;
            Text := Items[ iIndex ];
            end;//of else
       end;//of if
end;//of procedure

//------------------------------------------------------------------------------
procedure TAliasComboBox.AddSingleItemVal( sValue: String );
var
  iPos: Integer;
begin
  iPos := Pos( Items.NameValueSeparator,
               sValue );
  if( 0 < iPos )
  then AddAliasItem( Trim( Copy( sValue, 1, iPos - 1 )),
                     Trim( Copy( sValue, iPos + 1, Length( sValue ) - iPos )))
  else AddAliasItem( Trim( sValue ),
                     Trim( sValue ));
end;//of procedure

//------------------------------------------------------------------------------
procedure TAliasComboBox.AddItem( Item: String; aObject: TObject );
begin
  inherited AddItem( Item, aObject );

  AddSingleItemVal( Item );
end;//of procedure

//------------------------------------------------------------------------------
procedure TAliasComboBox.SetItems( const Value: TStrings );
begin
  AddAliasStrings( Value );
end;//of procedure

end.

Geändert von MiniMatrix ( 1. Aug 2014 um 11:01 Uhr)
  Mit Zitat antworten Zitat