How to skip one column and add string to another column in listview?
We create a listview with 5 columns. In one function we add data in first 3 cols in sequence. In the s开发者_如何学Goecond function we want to add string to the fifth col. Column 4 may be blank or filled before the second function. We just want to skip that column and add data in the fifth. Or the question may be boiled down to checking the fourth col' value. How to check it ? We tried if listview.items[i].SubItems.Strings[2]=''..., It will prompt out of bounds.
Please give some advice on it. Thank you in advance.
You cannot skip
a column in a TListView
when you use the item.SubItems property, but you can assign an empty string to a column.
you must proceed like this:
first always fill your columns when you add a new TItemList
.
item:=listview.Items.Add();
item.captiom:='Data col1';
item.SubItems.Add('Data col2');
item.SubItems.Add('Data col3');
item.SubItems.Add('');//empty
item.SubItems.Add('');//empty
then in your other functions, access any column to check whether it is empty or for adding data.
if listview.Items.Item[0].SubItems[2]<>'' then //check if the fourth column of the listiew is empty
listview.Items.Item[0].SubItems[2]:='Data col4' //adding the data
To add a string to the fifth column of the first item of a listview:
ListView_SetItemText(ListView1.Handle, 0, 4, 'fifth column');
To get the string in the fifth column of the first item of a listview:
var
ItemText: array [0..259] of Char;
begin
ListView_GetItemText(ListView1.Handle, 0, 4, @ItemText, SizeOf(ItemText));
ShowMessage(ItemText);
edit: Note that this method of adding a text to a subitem bypasses some VCL mechanism, for instance you can get a 'SubItem.Count' of '0' and still have the text on the subitem. If this is important I would use RRUZ's method.
精彩评论