Solved

ListBox problem

Posted on 1999-01-03
4
1,560 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

18 Experts available now in Live!

Get 1:1 Help Now