listbox加字符串_将带有字符串的字符串(或对象)存储在ListBox或ComboBox中

listbox加字符串

Delphi's TListBox and TComboBox display a list of items - strings in a "selectable" list. TListBox displays a scrollable list, the TComboBox displays a drop-down list.

Delphi的TListBox和TComboBox显示项目列表-“可选”列表中的字符串。 TListBox显示一个可滚动列表,TComboBox显示一个下拉列表。

A common property to all the above controls is the Items property. Items define a list of strings that will appear in the control to the user. At design-time, when you double-click the Items property, the "String List Editor " lets you specify string items. The Items property is actually a TStrings type descendant.

上述所有控件的共同属性是Items属性。 项目定义了将在控件中显示给用户的字符串列表。 在设计时,当您双击Items属性时,“字符串列表编辑器”可让您指定字符串项目。 Items属性实际上是TStrings类型的后代。

列表框中的每个项目两个字符串? ( Two Strings Per Item in a ListBox? )

There are situations when you want to display a list of strings to the user, for example in the list box control, but also have a way to store one more additional string along the one displayed to the user.

在某些情况下,您想向用户显示字符串列表 ,例如在列表框控件中,但是还有一种方法可以沿显示给用户的字符串存储另外一个字符串

What's more, you might want to store/attach more than just a "plain" string to the string, you might want to attach an object to the item (string).

而且,您可能不仅要在字符串中存储/​​附加一个“普通”字符串,还可能要在该项目(字符串)附加一个对象

ListBox.Items-TStrings“知道”对象! ( ListBox.Items - TStrings "Knows" Objects! )

Give the TStrings object one more look in the Help system. There's the Objects property which represents a set of objects that are associated with each of the strings in the Strings property - where the Strings property references the actual strings in the list.

在帮助系统中再给TStrings对象一个外观。 有一个Objects属性,它表示与Strings属性中的每个字符串相关联的一组对象-其中,Strings属性引用列表中的实际字符串。

If you want to assign a second string (or an object) to every string in the list box, you need to populate the Items property at run-time.

如果要为列表框中的每个字符串分配第二个字符串(或对象),则需要在运行时填充Items属性。

While you can use the ListBox.Items.Add method to add strings to the list, to associate an object with each string, you will need to use another approach.

尽管可以使用ListBox.Items.Add方法将字符串添加到列表中,以将对象与每个字符串关联,但是您将需要使用另一种方法。

The ListBox.Items.AddObject method accepts two parameters. The first parameter, "Item" is the text of the item. The second parameter, "AObject" is the object associated with the item.

ListBox.Items.AddObject方法接受两个参数。 第一个参数“ Item”是项目的文本。 第二个参数“ AObject”是与项目关联的对象。

Note that list box exposes the AddItem method which does the same as Items.AddObject.

请注意,列表框公开了与Item.AddObject相同的AddItem方法。

两弦一弦 ( Two Strings for One String )

Since both Items.AddObject and AddItem accept a variable of type TObject for their second parameter, a line like:

由于Items.AddObject和AddItem都将TObject类型的变量作为其第二个参数,因此类似以下行:

//compile error!
ListBox1.Items.AddObject('zarko', 'gajic');

will result in a compile error: E2010 Incompatible types: 'TObject' and 'string'.

将导致编译错误: E2010不兼容的类型:'TObject'和'string'

You cannot simply supply a string for the object since in Delphi for Win32 string values are not objects.

您不能简单地为对象提供字符串,因为在Win32的Delphi中,字符串值不是对象。

To assign a second string to the list box item, you need to "transform" a string variable into an object - you need a custom TString object.

要将第二个字符串分配给列表框项目,您需要将字符串变量“转换”为对象-您需要一个自定义的TString对象。

字符串的整数 ( An Integer for a String )

If the second value you need to store along with the string item is an integer value, you actually do not need a custom TInteger class.

如果需要与字符串项一起存储的第二个值是整数值,则实际上不需要自定义的TInteger类。

ListBox1.AddItem('Zarko Gajic', TObject(1973)) ;

The line above stores the integer number "1973" along with the added "Zarko Gajic" string.

上面的行存储整数“ 1973”以及添加的“ Zarko Gajic”字符串。

A direct typecast from an integer to an object is made above. The "AObject" parameter is actually the 4-byte pointer (address) of the object added. Since in Win32 an integer occupies 4 bytes - such a hard cast is possible.

上面是从整数到对象的直接类型转换。 “ AObject”参数实际上是所添加对象的4字节指针(地址)。 由于在Win32中,整数占用4个字节-这样的强制转换是可能的。

To get back the integer associated with the string, you need to cast the "object" back to the integer value:

要获取与字符串关联的整数,您需要将“对象”转换为整数值:

//year == 1973
year := Integer(ListBox1.Items.Objects[ListBox1.Items.IndexOf('Zarko Gajic')]) ;

字符串的Delphi控件 ( A Delphi Control for a String )

Why stop here? Assigning strings and integers to a string in a list box is, as you just experienced, a piece of cake.

为什么在这里停下来? 正如您刚刚经历的那样,为列表框中的字符串分配字符串和整数是小菜一碟。

Since Delphi controls are actually objects, you can attach a control to every string displayed in the list box.

由于Delphi控件实际上是对象,因此可以将控件附加到列表框中显示的每个字符串。

The following code adds to the ListBox1 (list box) captions of all the TButton controls on a form (place this in the form's OnCreate event handler) along with the reference to each button.

下面的代码将对窗体上所有TButton控件的标题添加到ListBox1(列表框)标题(将其放置在窗体的OnCreate事件处理程序中)以及对每个按钮的引用。

var
  idx : integer;
begin
  for idx := 0 to -1 + ComponentCount do
  begin
    if Components[idx] is TButton then ListBox1.AddObject(TButton(Components[idx]).Caption, Components[idx]) ;
  end;
end;

To programmatically "click" the "second" button, you can use the next statement:

要以编程方式“单击”“第二”按钮,可以使用下一条语句:

TButton(ListBox1.Items.Objects[1]).Click;

我想将自定义对象分配给字符串项 ( I Want to Assign My Custom Objects to the String Item )

In a more generic situation you would add instances (objects) of your own custom classes:

在更一般的情况下,您将添加自己的自定义类的实例(对象):

type
  TStudent = class
  private
    fName: string;
    fYear: integer;
  public
    property Name : string read fName;
    property Year : integer read fYear;
    constructor Create(const name : string; const year : integer) ;
  end;
........
constructor TStudent.Create(const name : string; const year : integer) ;
begin
  fName := name;
  fYear := year;
end;
--------
begin
  //add two string/objects -> students to the list
  ListBox1.AddItem('John', TStudent.Create('John', 1970)) ;
  ListBox1.AddItem('Jack', TStudent.Create('Jack', 1982)) ;
  //grab the first student - John
  student := ListBox1.Items.Objects[0] as TStudent;
  //display John's year
  ShowMessage(IntToStr(student.Year)) ;
end;

您创建的内容必须免费 ( What You Create You Must Free )

Here's what the Help has to say about objects in TStrings descendants: the TStrings object does not own the objects you add this way. Objects added to the TStrings object still exist even if the TStrings instance is destroyed. They must be explicitly destroyed by the application.

这是帮助有关TStrings后代中的对象的说明:TStrings对象不拥有以这种方式添加的对象。 即使销毁了TStrings实例,添加到TStrings对象的对象仍然存在。 它们必须由应用程序明确销毁

When you add objects to strings - objects that you create - you must make sure you free the memory occupied, or you'll have a memory leak

将对象添加到字符串(创建的对象)时,必须确保释放占用的内存,否则会发生内存泄漏

A generic custom procedure FreeObjects accepts a variable of type TStrings as its only parameter. FreeObjects will free any objects associated with an item in the string list In the above example, "students" (TStudent class) are attached to a string in a list box, when the application is about to be closed (main form OnDestroy event, for example), you need to free the memory occupied:

通用自定义过程FreeObjects接受类型为TStrings的变量作为其唯一参数。 FreeObjects将释放与字符串列表中的项目关联的所有对象。在上面的示例中,当应用程序即将关闭时(列表形式为OnDestroy事件,“学生”(TStudent类)附加到列表框中的字符串)。例如),您需要释放所占用的内存:

FreeObjects(ListBox1.Items) ;

Note: You only call this procedure when objects assigned to string items were created by you.

注意:仅当您创建了分配给字符串项目的对象时,才调用此过程。

翻译自: https://www.thoughtco.com/store-a-string-or-an-object-1058392

listbox加字符串

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值