Reputation: 394
I want to do a check on a ListBox if it is empty, like:
if {Listbox.Items is empty} then
begin
Listbox.Items.Add('Item');
end else
begin
//do somthing else
end;
The part of the check if Listbox.Items or if Listbox are/is empty is a little hard for me. I tried to figure out a way how to do it, but I failed as I am still a beginner with Delphi. How can I implement that in Delphi XE5?
Upvotes: 2
Views: 25262
Reputation: 1
I thing this will solve your problem
If List1.ListCount = 0 Then
Else
End If
Upvotes: 0
Reputation: 79
In Access VBA there is no ".items.count" property on a Listbox
I tried Me.ListBox.ListCount and .ListIndex to see if the List was empty.
ListCount was always 1 and ListIndex always -1 whether the list was empty or not (in my case).
To overcome that I used:
If Me.ListBox.ItemData(0) = "" then
Do Something
End If
This worked for me - hope this helps someone
Upvotes: 1
Reputation: 1399
I would reverse your if statement.
Personally I like the most code in the true part of my statement and the shorter code in the false part. For some reason it makes more sense to me.
So the code would look like:
If Listbox.items.count > 0
begin
//Do something else
end
else
Listbox.items.add('item');
Also if your true, or false, part only contains 1 line of code you don't need a begin..end. It's not wrong to have them but in my opinion it makes code easier to read if they aren't there ;)
Upvotes: 0