manipulating listbox.items

Hi experts,

I want to change a listbox.item which I copied from the
source-listbox to the target-listbox.

My code is as follows:

procedure TfrmDataInput.Button3Click(Sender: TObject);
begin
 if ListBox2.Items[ListBox2.ItemIndex] <> '' then {only if target-listbox is not empty ! }
  begin                                          
    frmEdit.Edit1.Text := ListBox2.Items[ListBox2.ItemIndex]; {load item to be changed into line editor}
    frmEdit.showmodal;  {display form with line editor}
    ListBox2.Items[ListBox2.ItemIndex]:=frmEdit.Edit1.Text; {copy new content in order to change listbox.item}
  end;                                                      
end;

But unfortunately, the line


if ListBox2.Items[ListBox2.ItemIndex] <> '' then

has an error. Delphi says that there is an exepction of class EStringListError.
The list index exceeds the limit (0).


Alternatively, I tried:

if ListBox2.ItemIndex <> -1

but I could not solve the problem.

Can you please tell me what I am doing wrong here?

With kind regards

Mathes

mathesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
slautinConnect With a Mentor Commented:
1)
    use exceptions...

2)
    try use this code:
    if listbox1.itemindex > 0 then

        .......

    Tlistbox.itemindex set value handly or if Tlistbox focused.

3)
    type in onCreate or onActivate methods:
        listbox1.itemindex := 0;

I did it ... worked.
     
Best regards.  
0
 
MadshiCommented:
Hmm. ListBox2.ItemIndex must have an invalid value. So what happened when you wrote "if ListBox2.ItemIndex <> -1"?
And please tell us, which value does ListBox2.ItemIndex have?

Regards, Madshi.
0
 
mathesAuthor Commented:
Dear experts,

in both cases

a) if ListBox2.Items[ListBox2.ItemIndex] <> '' then

or

b) if ListBox2.ItemIndex <> -1


I get an error message from Delphi:

Delphi says that there is an exepction of class EStringListError.
The list index exceeds the limit (0).

When I watched the variable ListBox2.ItemIndex, I found out that this variable has the value 0.

This is the reason why I changed the line in my code to

if ListBox2.Items[ListBox2.ItemIndex] <> 0

If now the target-listbox is empty, everything works perfectly. The command button

"change item in tarhet-listbox" is totally paralyzed.

However this command button unfortunately is paralyzed too if I select exactly one item.
And this is of course a major bug, because with one entry in the target-litbox it makes
sense to alllow the user to edit and change this item.

If the target list has exactly 2 entries the szenario is as follows:

If the target list looks like this

first_item
second_item

and I try to change "first_item"

the command button is paralyzed, so if you click on it with the mouse,
it happens nothing !!!

And if you try to change the second item from "second_item" to "second_item_new"
not only the second item is changed, but unfortunately the first item is changed too.
It seems to me that the "first_item" is overwritten ;-(((((

After changing the second item, the target-listbox has this shape:

second_item_new
second_item_new

Obviosly both items of the target_list are identical, although I changed only on of these
entries in the target-listbox.

This is all very strange for me. I don't understand the behaviour of this source code.

Can you please try to find the bug in this source?

With kind regards

Mathes

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
MatveyCommented:
Listbox behavour is described by these properties:

-The strings it contains:
      Items[0..Items.count-1]

-The selected item index:
      ItemIndex = 0..Items.count-1, or (-1) if nothing is selected

-The number of the items: Items.Count

So the errors you have:

  ListBox2.Items[ListBox2.ItemIndex] <> ''

-is incorrect. To check if there are items, check the Count property. Also it is possible that no current item is selected, os the statement is irrelevant...
0
 
ZifNabCommented:
Matvey, if I understood the question correctly, then I've to agree with you
0
 
mathesAuthor Commented:
Hi experts,


Matvey:

if I use the items.count, the command button correctly is paralyzed if the target-listbox is empty.
However, if the target-listbox is not empty, the editing of a selected listboxitem does not work
correctly.

For example: If the shape of the target-listbox is

first_listboxitem
second_listboxitem


and I change second_listboxitem to second_listboxitem_new

the target-listbox will look like this:

second_listboxitem_new
second_listboxitem_new

Can you please tell me, why first_listboxitem is changed, too, although I did not select it?
Actually I want a target-listbox like this:


first_listboxitem
second_listboxitem_new


Here is one again my code:


procedure TfrmDataInput.Button3Click(Sender: TObject);
begin
  if ListBox2.Items.Count > 0 then
  begin
    frmEditCondition.Edit1.Text := ListBox2.Items[ListBox2.ItemIndex];
    frmEditCondition.showmodal;
    ListBox2.Items[ListBox2.ItemIndex] := frmEditCondition.Edit1.Text;
  end;
end;


slautin:

Thank you for your hints. Can you please provide me with your demo program?
I don't understand what you want to explain to me. But I think a little demo
program will clarify what you want to tell me.


With kind regards

Mathes

0
 
MatveyCommented:
Obvoiusly, this code, or something else that you didn't notice, executes twice, because you change the item only once in the procedure you've shown. Try to trace the execution to see what causes it to execute twice.
0
 
mathesAuthor Commented:

Hi experts,

I want to inform you that I meanwhile could fix the bug.

Here you see my correct source code:

procedure TfrmDataInput.Button7Click(Sender: TObject);
var  {change selected item}
  ListIndex: integer;
begin
if ListBox4.Items.Count > 0 then
  begin
    for ListIndex := 0 to ListBox4.Items.Count - 1 do
      if ListBox4.Selected[ListIndex] = true then
      begin
        frmEditOption.Edit1.Text := ListBox4.Items[ListBox4.ItemIndex];
        frmEditOption.showmodal;
        ListBox4.Items[ListBox4.ItemIndex] := frmEditOption.Edit1.Text;
      end;
  end;
end;


With kind regards

Mathes

0
 
mathesAuthor Commented:
Hi experts,

thank you all for your help. Everything works very well now.

With kind regards

Mathes
0
All Courses

From novice to tech pro — start learning today.