如何将 ItemIndex 添加到 TRibbonComboBox?

发布于 2024-08-04 06:40:22 字数 755 浏览 1 评论 0原文

我刚刚发现 Delphi TRibbonComboBox 没有项目索引,但它应该有。

我想至少在单元中本地修复这个问题,并且我认为 Delphi 2009 添加了一种向外部类引入新方法而不必从类中继承的方法,但我不记得如何实现。

有没有办法添加“function ItemIndex:integer;”至少在本地单元内添加到 TRibbonComboBox 类,而不必弄乱原始组件? (或者我在想 C#?)

谢谢!

这是答案/实现,谢谢梅森!

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-08-11 06:40:22

您可以使用类帮助器,如下所示:

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

需要注意的是,您不能以这种方式添加任何新字段,因此您必须能够根据 TRibbonComboBox 公开提供的信息计算此函数的返回值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文