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
Solved

manipulating listbox.items

Posted on 1998-12-25
9
416 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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