How to create a combobox with two columns (one hidden) in Delphi 7?

前端 未结 2 1472
庸人自扰
庸人自扰 2021-01-02 18:56

How to create a TComboBox with two columns that has one of its columns hidden so that it can keep an id value along with the actual item in it? And then how to get

2条回答
  •  星月不相逢
    2021-01-02 19:48

    ComboBox controls do not support columns, and you do not need a hidden column anyway to accomplish what you need.

    The TComboBox.Items property is a TStrings descendant. It can hold both string values and associated user-defined data values together at the same time, but the user will only see the string values in the UI. Use the Items.AddObject() method to add string+ID values to the list, and then use the Items.Objects[] property to retrieve the ID values when needed.

    Alternatively, you could just store your ID values in a separate array that has the same number of elements as the ComboBox and then use the ComboBox item indexes to access the array values. This is especially important if you need to store a value of -1, because that particular value is not retrievable from the Objects[] property of a TComboBox due to the way the getter method is implemented, like Rob said.

提交回复
热议问题