TAdvGridDropDown Helper

When Christmas goes. Such a trifle. Maybe someone will please:
  •  The main reason was to keep the column widths as set by the user. To do this, TMS modified the component
  •  SetDropDownHeight
  •  SetItemIndex
  •  ClearAdvGridDropDown
  •  IndexOf
  •  Automatic adjustment of the number of displayed rows. Now I have a maximum of 21
  •  Adjust row height to classic
Aby fungovalo je nutn?
procedure TfrmChartOfAccounts.advgddClassGridColumnSizing(Sender: TObject; ACol, ColumnSize: Integer);
begin
  advgddClass.Columns[ACol].Width := ColumnSize;
end;

unit GridDropDownHelper;  // class helper for TAdvGridDropDown

interface

uses
  System.Classes, System.Generics.Collections, System.SysUtils, AdvGridDropDown;

type
  TAdvGridDropDownHelper = class helper for TAdvGridDropDown
  strict private
    procedure FreeAndNilGridObjects;
  public
    procedure ClearAdvGridDropDown;
    function GetID(const AIndexItems: Integer): Integer;
    procedure IndexOf(const AText: string);
    procedure SetDropDownHeight;
    procedure SetItemIndex(const AValue: Integer);
  end;

implementation

uses
  DeclarationTypes, BasicFunctions;

{ TGridDropDown }

procedure TAdvGridDropDownHelper.FreeAndNilGridObjects;
var
  Obj: TObject;
  I: Integer;
begin
  for I := Self.Items.Count -1 downto 0 do
  begin
    if Self.Items.Text.Count > 0 then
    begin
      Obj := Self.Items.Text.Objects[0];
      Obj.Free;
    end;
  end;
end;

procedure TAdvGridDropDownHelper.ClearAdvGridDropDown;
var
  I: Integer;
begin
  Self.Items.BeginUpdate;

  try
    Self.Clear;
    FreeAndNilGridObjects;

    for I := 0 to Self.Items.Count -1 do
      Self.Items.Text.Clear;

    Self.Items.Clear;
    Self.Text := '';
    Self.SetDropDownHeight;
  finally
    Self.Items.EndUpdate;
  end;
end;

function TAdvGridDropDownHelper.GetID(const AIndexItems: Integer): Integer;
var
  Obj: TObject;
  StringList: TStringList;
  vList: TList<Variant>;
  iList: TList<Integer>;
  ItemIndex: Integer;
begin
  Result := 0;
  ItemIndex := Self.ItemIndex;

  if ItemIndex = -1 then
    Exit;

  Obj := Self.Items[ItemIndex].Text.Objects[0];

  if Obj = nil then
    Exit;

  try
    if Obj is TInteger then
      Result := TInteger(Obj).Value
    else
    if Obj is TStringList then
    begin
      StringList := TStringList(Obj);
      Result := StrToInt(StringList[0]);
    end
    else
    if Obj is TList<Integer> then
    begin
      iList := TList<Integer>(Obj);
      Result := iList[AIndexItems];
    end
    else
    if Obj is TList<Variant> then
    begin
      vList := TList<Variant>(Obj);
      Result := vList[0];
    end
    else
    begin
      FaultDialog('Getting the primary key', 'Please try again later.', 'Error getting ID from Self-u ' + Self.Name, '', '');
      Result := 0;
    end;
  except
      FaultDialog('Getting the primary key', 'Please try again later.', 'Error getting ID from Self-u ' + Self.Name, '', '');
    Result := 0;
  end;
end;

procedure TAdvGridDropDownHelper.IndexOf(const AText: string);
var
  I, Idx: Integer;
begin
  for I := 0 to Self.Items.Count -1 do
  begin
    Idx := (Self.Items.Items.Text.IndexOf(AText));

    if Idx > -1 then
    begin
      Self.ItemIndex := I;
      Break;
    end;
  end;
end;

procedure TAdvGridDropDownHelper.SetDropDownHeight;
const
  iRowCount = 21;
  ForScrollbar = 2;
var
  RowCount, DropDownHeight: Integer;
begin
  RowCount := Self.RowCount;

  if RowCount > iRowCount then
    RowCount := iRowCount;

  RowCount := RowCount + ForScrollbar;  // One row is reserve for the scrollbar
  DropDownHeight := RowCount * Self.DropDownRowHeight;

  if Self.DropDownHeader.Visible then
    DropDownHeight := DropDownHeight + Self.DropDownHeader.Height;

  if Self.DropDownFooter.Visible then
    DropDownHeight := DropDownHeight + Self.DropDownFooter.Height;

  Self.DropDownHeight := DropDownHeight;
//  Self.DropDownHeight := RowCount * Self.DropDownRowHeight + Self.HeaderHeight + Self.DropDownFooter.Height +
//    Self.DropDownHeader.Height;
end;

procedure TAdvGridDropDownHelper.SetItemIndex(const AValue: Integer);
var
  I: Integer;
  NotifyEvent: TNotifyEvent;
begin
  Self.ItemIndex := -1;

  if (Self.Items.Count = 0) or (AValue = 0) then
    Exit;

  NotifyEvent := Self.OnChange;
  Self.OnChange := nil;

  try
    for I := 0 to Self.Items.Count -1 do
    begin
      Self.ItemIndex := I;

      if AValue = Self.GetID(0) then
        Break;
    end;
  finally
    Self.OnChange := NotifyEvent;
  end;
end;

end.

That is nice!
Thanks for sharing!

correction on line 72

  if (Self.Items.Count = 0) or (Self.ItemIndex = -1) then

I thought if Self.Items is empty, then ItemIndex = -1. It's not true