Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

manipulating listbox.items

Posted on 1998-12-25
9
Medium Priority
?
430 Views
Last Modified: 2010-04-04
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

0
Comment
Question by:mathes
9 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1352987
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
 

Author Comment

by:mathes
ID: 1352988
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1352989
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1352990
Matvey, if I understood the question correctly, then I've to agree with you
0
 
LVL 1

Accepted Solution

by:
slautin earned 200 total points
ID: 1352991
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
 

Author Comment

by:mathes
ID: 1352992
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1352993
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
 

Author Comment

by:mathes
ID: 1352994

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
 

Author Comment

by:mathes
ID: 1352995
Hi experts,

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

With kind regards

Mathes
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question