【问题标题】:Add a combobox in to listview control in c++ builder在 c++ builder 中向 listview 控件添加一个组合框
【发布时间】:2013-02-04 09:19:04
【问题描述】:

我想创建一个包含 2 列的列表视图。在第一列中它必须是行号,在第二列中它应该包含一个组合框。我编写了以下代码,但第二列仅显示“组合”字符串。它不显示任何组合框。怎么了?

for (int   i = 0; i < 10; i++) {

TListItem *items;
items= this->ListView1->Items->Add();
items->Caption=IntToStr(i);

items->SubItems->AddObject("combo"+IntToStr(i),(TObject *)this->ComboBox1);

}

【问题讨论】:

  • 请注意,尽量避免 C-cast 并使用 C++ casts

标签: c++builder vcl


【解决方案1】:

它没有显示TComboBox,因为您实际上还没有将TComboBox 设置为TListView 的子控件。您所做的只是将TComboBox 指针存储为与TListItem 关联的用户定义值。这对 UI 没有影响,所以去掉它:

for (int i = 0; i < 10; i++)
{
    TListItem *items = ListView1->Items->Add();
    items->Caption = IntToStr(i);
    items->SubItems->Add("combo"+IntToStr(i));
}

要在TListView 中实际显示TComboBox,您必须将TListView 分配为TComboBoxParent,然后使用SetBounds() 方法来定位和调整@ 987654333@ 随时显示:

ComboBox1->Parent = ListView1;
...
RECT rect = {0};
ListView_GetSubItemRect(ListView1->Handle, SomeListItem->Index, 1, LVIR_BOUNDS, &rect);
ComboBox1->SetBounds(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
// update ComboBox1->Items as needed...
ComboBox1->Visible = true;
...
ComboBox1->Visible = false;

话虽如此,您尝试做的事情最好使用TValueListEditor 组件来处理。根据需要向其中添加项目,然后使用其ItemProps 属性将每个项目的TItemProp.EditStyle 属性设置为esPickList,然后根据需要使用TValueListEditor.OnGetPickList 事件或TItemProp.PickList 属性来管理ComboBox 字符串。

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2015-11-01
    • 2021-09-11
    相关资源
    最近更新 更多