Solved

ListBox problem

Posted on 1999-01-03
4
1,568 Views
Last Modified: 2012-06-27
Hi...  
  I'm using Delphi 3 and I have an app that uses a TListBox with the sorted property set to true. I have it coded so that I can double-click on any item in the ListBox and I can edit it using a simple inputbox. That works ok, problem is, after I edit an item and change it to something else, the list does not automatically re-sort itself. The new edited item stays just where it was before even though it is now out of alphabetical order! Can you help?
  In a related question concerning TListBoxes...can they be set so that no duplicate items can be entered? Or can this only be done with a TStringList?
 
Thanks
  Shawn Halfpenny
  drumme59@sprint.ca
0
Comment
Question by:aztec
[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
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
slautin earned 50 total points
ID: 1354333
for resorted use:

var i : integer;
    s : string;
begin
    s := ListBox1.Items[ListBox1.ItemIndex];
    ListBox1.Sorted:=False;
    ListBox1.Sorted:=True;
    for i := 1 to ListBox1.ItemIndex do
       if ListBox1.Items[i] := s then
           break;
    ListBox1.ItemIndex := i;

For no duplicate:

var i : integer;
    can_add : boolean;
begin
    can_add := true;
    for i := 1 to ListBox1.ItemIndex do
       begin
       if ListBox1.Items[i] := newval then
           begin
           can_add := false;
           break;
           end;
       end;
    if can_add then
       ListBox1.Items.Add[newval];

......

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354334
Here is the simplest way,,,

ListBox.Items[ListBox.ItemIndex] := 'Whatever';
ListBox.Sorted := False;
ListBox.Sorted := True;

Now everything would be sorted....

If oyu dont want to have duplicates in your listbox as slautin prposed, you can do one of two ways....

the first is this,,,,

if ListBox.Items.IndexOf('Whatever') < 0 then begin
  ListBox.Items[ListBox.ItemIndex] := 'Whatever';
  ListBox.Sorted := False;
  ListBox.Sorted := True;
end;

This was the first way w/o having duaplicates.... The second way is to do this...

var
  List : TStringList;
begin
  List := TStringList.Create;
  List.Assign(ListBox.Items);
  List.Duplicates := dupIgnore;
  List.Sort := True;
  ListBox.Items.Assign(List);
  List.Free;
end;

This would remove the duplicates and will sort the items and put them back in the listbox...

Hope this helps...

-Viktor
--Ivanov
0
 

Author Comment

by:aztec
ID: 1354335
Great Thanks! And thanks to Viktor for his comments.

Shawn
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354336
No problem... If you are still having problems let me know...
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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 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