views:

218

answers:

1

I just discovered that the Delphi TRibbonComboBox doesn't have an item index, and it should.

I'd like to fix this locally at least for the unit, and I think Delphi 2009 added a way to introduce new methods to an outside class without having to descent from the class, but I can't remember how.

Is there a way to add 'function ItemIndex: integer;' to the TRibbonComboBox class at least within the local unit with out having to mess with the original component? (Or am I thinking C#?)

Thanks!

Here's the answer/implementation, thx Mason!

TRibbonComboBoxHelper = class helper for TRibbonComboBox
public
  function GetItemIndex: integer;
  procedure SetItemIndex(Index : integer);
  property ItemIndex : integer read GetItemIndex write SetItemIndex;
end;

function TRibbonComboBoxHelper.GetItemIndex: integer;
begin
  result := Items.IndexOf(Text);
end;

procedure TRibbonComboBoxHelper.SetItemIndex(Index: integer);
begin
  if (Index >= 0) and (Index < Items.Count) then
    Text := Items[Index];
end;
+2  A: 

You can use a class helper, like this:

type
  TRibbonComboBoxHelper = class helper for TRibbonComboBox
  public
    function ItemIndex: integer;
  end;

The caveat is that you can't add any new fields this way, so you have to be able to calculate the return value of this function from information publicly available from TRibbonComboBox.

Mason Wheeler