ListBox Component (TListBox)
This component is a standard Windows list box component that displays a series of lists where users can select, join, delete, or modify items.
1. Typical usage of TLlistBox components
List boxes are often used in Windows applications. The list box can list multiple items in a rollable window for users to choose. For example, in Delphi's IDE, when using the Component List command on the View menu, Delphi lists all components in the VCL. The program mainly uses the Items attribute to control various items, such as using Items.Strings[] to get the content of a certain option, and Items.Add() and Items.Delete() add or delete option entries. Specify or get the current option through the ItemIndex property.
2. Important properties of TListBox components
·Columns list box number ·ExtendedSelect This property is used to set whether to allow multiple items to be selected simultaneously using the Shift key and the Ctrl key ·IntegralHeight The height of the list box is always an integer multiple of the height of the item in the list box ·ItemHeight This property is used to Set the height of each item in the list box ·ItemIndex This property is used to set the sequence number of the selected items in the list box ·Items All items in the list box are accessed through the Items property ·MultiSelect Allows multiple items to be selected at the same time ·SelCount Return the number of currently selected items ·Selected This property is used to determine whether the specified item is selected ·Sorted This property is used to set whether the items in the list box should be sorted alphabetically ·Style This property is used to set the style of the list box The following describes the commonly used properties of the TListBox component in the program.
(1) Columns attributes
The default value of this property is 0, and the list box is displayed in a single column. If there is too much content, the list box will automatically add a vertical scroll bar. If this property is set to a value greater than 0, the list box will first fill in a column. If it cannot be fully displayed in one column, it will be displayed in column 2, column 3, and so on, but only the number of columns set by the Columns property is displayed on the screen at most. If this property is set to 2, only two columns will be displayed on the screen. If the actual content is more than two columns, a horizontal scroll bar will be automatically added.
(2) ExtendedSelect property
This property is used to set whether to allow multiple items to be selected simultaneously with the Shift key and Ctrl key, provided that the MultiSelect property must be set to True. If the ExtendedSelect property is set to True, multiple items can be selected when the user presses and holds the Shift key. To select multiple items (not necessarily continuous).
(3) IntegralHeight property
If this property is set to True and the Style property is set to IsOwnerDrawFixed, the height of the list box will automatically be adjusted to an integer multiple of the height of the item in the list box. If this property is set to False, the last item in the list box may not be displayed in full, because the height of the list box is not necessarily exactly an integer multiple of the ItemHeight property.
(4) ItemHeight attribute
When the Style property is set to IsOwnerFixed (user-defined), this property is used to set the height of each item in the list box. If the IntegralHeight property is set to True, the height of the entire list box will be automatically adjusted with the value of the ItemHeight property. If the Style property is set to IsStandard or IsOwnerDrawVariable, the ItemHeight property will be meaningless.
(5) ItemIndex attribute
This property is used to set the sequence number of the selected item in the list box. If the MultiSelect property is set to True and the user selects multiple items, this property returns the sequence number of the item with the input focus. The program sample code is as follows:
[delphi] view plaincopyPRocedureTForm1.FormCreate(Sender:TObject);varI:Integer;beginListBox1.MultiSelect:=False;Button.Caption='MovetoTop';forI:=1to10doListBox1.Items.Add('Item'+IntToStr(I)) ;end;procedureTForm1.Button1Click(Sender:TObject); beginListBox1.Items.Move(ListBox1.ItemIndex,0); end;
(6) Items attributes
All items in the list box are accessed through the Items property. The Items property is a typical TStrings object. Through this object, items in the list box can be manipulated during the runtime. The program sample code is as follows:
[delphi] view plaincopyvarF:file;i:integer;beginfori:=0to(FileListBox1.Items.Count-1)dobegintryifFileListBox1.Selected[i]thenbeginifnotFileExists(FileListBox1.Items.Strings[i])thenbeginMessgeDlg('File:'+FileListBox1 .Items.Strings[i]+'notfound',mtError,[mbok],0);continue;end;AssignFile(F,FileListBox1.Items.Strings[i]);Reset(F,l);ListBox1.Items. Add(IntToStr(FileSize(F)));CloseFile(F); end; finally{dosomethinghere}end;end; end;
(7) MultiSelect property
If this property is set to True, multiple items are allowed to be selected simultaneously. Users can hold down the Ctrl or Shift keys, select items with the mouse, or directly drag and drop with the mouse to select.
(8) Sorted properties
This property is used to set whether the items in the list box should be sorted alphabetically. For sorted list boxes, the items added by calling Add() or Insert() will automatically move to the appropriate place. The program sample code is as follows:
[delphi] view plaincopyprocedureTForm1.FormCreate(Sender:TObject); beginListBox1.Items.Add('Item2');ListBox1.Items.Add('Item3');ListBox1.Items.Add('Item1') ;ListBox1.Items.Add('Item4');end;
(9) Style attributes
This property is used to set the style of the list box. It sets the following 3 user-selectable values. • lbStandard: Each item is a string, and each item has the same height. • lbOwnerDrawFixed: The item is not necessarily a string, but can also be an image. The height of the item is specified by the ItemHeight property. The OnDrawItem event is triggered before each item is displayed. • lbOwnerDrawVariable: The item is not necessarily a string, it can also be an image, and the height of the item can be different. Two events are triggered before each item is displayed. The first is the OnMeasureItem event, let the program specify the height of each item, and the second is the OnDrawItem event, let the program draw each item.
3. Important events of TListBox components
·OnDrawItem If the Style property is set to lbOwnerDrawFixed or lbOwnerDrawVariable and the items in the list box need to be repainted, the event will be triggered. OnMeasureItem If the Style property is set to lbOwnerDrawVariable and the items in the list box need to be repainted, the event will be triggered
4. Important ways to TListBox components
The commonly used method of the TListBox component in programs is Clear, which is the function of deleting all options in the list box.
How does the item in Listbox display different colors according to the content
1. Change the Style property of ListBox1 to lbOwnerDrawVariable2. In the OnDrawItem event of ListBox, change the Canvas property according to the value of the item.
For example:
[delphi] view plaincopyprocedureTForm1.ListBox1DrawItem(Control:TWinControl;Index:Integer;Rect:TRect;State:TOwnerDrawState); begin//The font uses the original default color ifOdd(index)then//The color when the index of items is an odd number beginlistbox1.Canvas.Brush.Color:=clwindow;ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);endelse//The color beginlistbox1.Canvas when the index of items is even number .Brush.Color:=clinactivecaptiontext;ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);end;ifodSelectedinstatethen//The color when selected beginlistbox1.Canvas.Brush.Color:= clhighlight;ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);end;end;