Solved

manipulating listbox.items

Posted on 1998-12-25
9
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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 50 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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