Solved

manipulating listbox.items

Posted on 1998-12-25
9
405 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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352990
Matvey, if I understood the question correctly, then I've to agree with you
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now